I'm a student enrolled in a University beginners programming course.
The problem is:
Write a program capable of using month and day of a given date to calculate the number of days from january 1 that it represents. Make the program capable of computing values for as many as 20 dates.
Data should be input from a data file called DAYS.DAT. The data file should have the following format:

line 1 n (number of dates to be computed)
line 2 month day
line 3 month day
line n+1 month day (all data are integers)

an example data file is :
5
12 7
8 5
1 27
4 18
7 22
print your results to file DAYS.OUT in the following format
Table of dates and days from January 1
Date Days from Jan. 1
December 7 ...
August 5 ...
January 27 ...
etc...

not that you must display thee month in words not numbers

This is what I have so far.. I haven't gotten to putting in the math of the january dates I just want it to output that each of the days is December 7 August 5 and so on... but my output is currently nothing.. it doesn't repeat like i thought it would.. i'm really confused.. only outputs December 7
I know the answer is either do..while, while, or for loops because that is what our chapter is on.. can you guys please give me some hints?

#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>

using namespace std;
int main()

{
	int x, month, day;

	ofstream outfile ("C:\\DAYS.OUT");
	outfile<<"Table of dates and days from January 1"<<endl;
	outfile<<setiosflags(ios::left)<<setw(20)<<"Date";
	outfile<<setiosflags(ios::left)<<"Days from Jan. 1"<<endl;
	outfile.close();

	ifstream infile ("C:\\DAYS.DAT");
	infile>>x;	

	for (x; x<=20&&x>0; x--)
		{
			infile>>month>>day;
			infile.close();

			ofstream outfile ("C:\\DAYS.OUT");
		
			if (month==12,)
				{
				outfile<<"December "<<day;
				outfile.close();
				}
			else if (month==11)
				{
				outfile<<"November "<<day;
				outfile.close();
				}
			else if (month==10)
				{
				outfile<<"October "<<day;
				outfile.close();
				}
			else if (month==9)
				{
				outfile<<"September "<<day;
				outfile.close();
				}
			else if (month==8)
				{
				outfile<<"August "<<day;
				outfile.close();
				}
			else if (month==7)
				{
				outfile<<"July "<<day;
				outfile.close();
				}
			else if (month==6)
				{
				outfile<<"June "<<day;
				outfile.close();
				}
			else if (month==5)
				{
				outfile<<"May "<<day;
				outfile.close();
				}
			else if (month==4)
				{
				outfile<<"April "<<day;
				outfile.close();
				}
			else if (month==3)
				{
				outfile<<"March "<<day;
				outfile.close();
				}
			else if (month==2)
				{
				outfile<<"February "<<day;
				outfile.close();
				}
			else if (month==1)
				{
				outfile<<"January "<<day;
				outfile.close();
				}
		} 
}

Recommended Answers

All 10 Replies

What are you trying to accomplish with this statement? if (month=12, month<=3)

nothing sorry, i just removed that.. was from an earlier attempt using for loops inside... now my output is December 7

Let's look at your program.

Open input file
Start a loop up to 20 times
    input from the file
    close input file
    process input
end of loop

So, since the input file is closed the second time through the loop, your input causes an error which you never test for. Then you just blindly process whatever is in the input variables (which is the values from the first and only valid input)

then do i need to close the file outside of the loop where i define x and then open the file inside of the loop to define month and day?

well that didn't work either.. it gave me an output of May 12.. it read the x value of 5 as a month value.. anyone else got any ideas?

then do i need to close the file outside of the loop where i define x and then open the file inside of the loop to define month and day?

No. You need to think about what opening and closing a file means. When you open a file what's the first thing to be read?

the first number .. ok.. i see.. i thought it would move to the next number the next time i open the file... but i'm guessing it will only move to the next number while the file is open and if I close it, then it starts over... so then how do i get it to define my variables in a way that i can get a loop working... i mean do i have to create a month 1, day1, month2, day2.. kind of scheme or is there a way to get it to re-input the variables with the next number every time the loop runs? i'm really lost here.

the first number .. ok.. i see.. i thought it would move to the next number the next time i open the file... but i'm guessing it will only move to the next number while the file is open and if I close it, then it starts over...

Exactly.

so then how do i get it to define my variables in a way that i can get a loop working...

It's already defined.

i mean do i have to create a month 1, day1, month2, day2.. kind of scheme or is there a way to get it to re-input the variables with the next number every time the loop runs? i'm really lost here.

Maybe you need to step back and write a test program that ONLY reads the file and outputs the data read. Should only take a few minutes and it will explain a lot (I hope).

Key point in programming:
If you have a task and there are 3 things you don't quite understand, don't write the program in one shot. Write 3 programs one at a time exploring each of the things you don't understand. Only when you understand each separately, put them together to solve the whole task.

alright i get the input and output for the most part .. i think .. and i rewrote some of the code..

#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>

using namespace std;
int main()

{
	int x, month, day;

	ofstream outfile ("C:\\DAYS.OUT");
	outfile<<"Table of dates and days from January 1"<<endl;
	outfile<<setiosflags(ios::left)<<setw(20)<<"Date";
	outfile<<setiosflags(ios::left)<<"Days from Jan. 1"<<endl;

	ifstream infile ("C:\\DAYS.DAT");
	infile>>x;	

	for (x; x!=0&&x<=20; x--)
		{
			infile>>month>>day;
				if (month==12)
				{
				outfile<<"December "<<day<<endl;
				outfile.close();
				}
				if (month==11)
				{
				outfile<<"November "<<day<<endl;
				outfile.close();
				}
				if (month==10)
				{
				outfile<<"October "<<day<<endl;
				outfile.close();
				}
				if (month==9)
				{
				outfile<<"September "<<day<<endl;
				outfile.close();
				}
				if (month==8)
				{
				outfile<<"August "<<day<<endl;
				outfile.close();
				}
				if (month==7)
				{
				outfile<<"July "<<day<<endl;
				outfile.close();
				}
				if (month==6)
				{
				outfile<<"June "<<day<<endl;
				outfile.close();
				}
				if (month==5)
				{
				outfile<<"May "<<day<<endl;
				outfile.close();
				}
				if (month==4)
				{
				outfile<<"April "<<day<<endl;
				outfile.close();
				}
				if (month==3)
				{
				outfile<<"March "<<day<<endl;
				outfile.close();
				}
				if (month==2)
				{
				outfile<<"February "<<day<<endl;
				outfile.close();
				}
				if (month==1)
				{
				outfile<<"January "<<day<<endl;
				outfile.close();
				}
		}
}

but it still only outputs:
Table of dates and days from January 1
Date Days from Jan. 1
December 7

how do i get it to output the rest of the dates?

...o wait i'm still closing the outfile...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.