Quantcast

Maximum PC

It is currently Thu Dec 25, 2014 3:11 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Basic c++ help
PostPosted: Tue Sep 14, 2004 5:56 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
How do you save the output of a program to file?

Code in next post, my book isn't very helpfult. I know to use fstream.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 5:57 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
#include <string>
#include <iostream>


using namespace std;


void main ()
{
int day, year, month, a, y, m;
string day_of_week;


cout << "Enter the year (e.g. 2000): ";
cin >> year;
cout <<endl;
cout << "Enter the month (1-12): ";
cin >> month;
cout <<endl;
cout << "Enter the day (1-31): ";
cin >> day;
cout<<endl;

a = (14 - month ) / 12;

y = year - a;

m = month + y*a - 2;

day = (day + y + y/4 - y/100 + y/400 + 31*m/12) % 7;

if(day==0) {
day_of_week = "Sunday";
}else if(day==1) {
day_of_week = "Monday";
}else if(day==2) {
day_of_week = "Tuesday";
}else if(day==3) {
day_of_week = "Wednesday";
}else if(day==4) {
day_of_week = "Thursday";
}else if(day==5) {
day_of_week = "Friday";
}else if(day==6) {
day_of_week = "Saturday";
}


cout << month << "/" << day << "/" << year << " falls on a " << day_of_week <<endl;



}


Top
  Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 7:48 pm 
I judge you GUILTY!
I judge you GUILTY!
User avatar

Joined: Tue May 25, 2004 4:38 pm
Posts: 162
Location: New York City
What is the assignment first?
Given the nature of your assignment, it seems that you don't have to do any real file processing.

However, some commentary:

In C++, it may be legal to use void main() but you should never use this in a real program. It should always be int main() or int main(int argc, char* argv[])

The variable a should not be an integer. It should be a double. Otherwise a will always be zero. In fact, since A is always zero, it has no use in your calculations.

Have you checked the correctness?

Also after every cin statement you shouldn't have a cout to skip a line.
Your output would like this:
Enter a month: 12

Enter a day: 31

etc.

While it may look neater on screen, it makes the code unnecessarily long.

Anyhow, to get the output to a file without coding it:
Do this
executablefilneame > output.txt

You also have another logic bug.
At the end, you output the result. You output the day variable which you modfied. So if I enter the 31st, it will be modified to be a number from 0-7.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 3:57 am 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Yeah, that was the wrong code. I'll check and see if that works in a minute and try it though.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 9:32 am 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
#include <string> // String header file
#include <iostream> // Input Output stream header file
#include <fstream>

using namespace std; Standard included statement


void main ()
{

int day, year, month, weekday, y, m; float a;
string day_of_week;
char ch;




cout << "Enter the year (e.g. 2000): ";
cin >> year;
cout <<endl;
cout << "Enter the month (1-12): ";
cin >> month;
cout <<endl;
cout << "Enter the day (1-31): ";
cin >> day;
cout<<endl;


a = (14 - month)/12;

y = year - a;

m = month + 12*a - 2;

weekday = (day + y + y/4 - y/100 + y/400 +31*m/12) % 7;
if(weekday==0) {
day_of_week = "Sunday";
}else if(weekday==1) {
day_of_week = "Monday";
}else if(weekday==2) {
day_of_week = "Tuesday";
}else if(weekday==3) {
day_of_week = "Wednesday";
}else if(weekday==4) {
day_of_week = "Thursday";
}else if(weekday==5) {
day_of_week = "Friday";
}else if(weekday==6) {
day_of_week = "Saturday";
}


cout << month << "/" << day << "/" << year << " falls on a " << day_of_week <<endl<<endl;


cin.get(ch);




}


Top
  Profile  
 
 Post subject: Add this
PostPosted: Wed Sep 15, 2004 3:24 pm 
8086
8086

Joined: Wed Sep 15, 2004 2:35 pm
Posts: 2
Alex,

Add this: cin.ignore();

before the cin.get(ch);


Top
  Profile  
 
 Post subject: Re: Add this
PostPosted: Wed Sep 15, 2004 3:41 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
YubinYankin wrote:
Alex,

Add this: cin.ignore();

before the cin.get(ch);


Thank you!!! I owe you one.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Sep 18, 2004 12:59 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Hello,

Just a heads up. This forum has bbcode available, so in the future, please use the code tag when displaying code. Makes everyones life a little bit easier.

example:
Code:
#include <string>      // String header file
#include <iostream>   // Input Output stream header file
#include <fstream>
using namespace std;                           

void main ()   
{                                       
    int day, year, month, weekday, y;
    float a;
    string day_of_week;                           
    char ch;
...


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.