End of file loop problem?

Hi guys. I'm in just an introductory c++ class. A few weeks ago we had a project where we had to write a program that, given a file with a date read in MM DD YYYY, would calculate the number day of the year. And apply an extra day if it's a leap year. They wanted us to use 3 value returning functions. Now, however, they want us to go back and edit our program so that given a file with a list of dates will just use an end of file loop to just keep reading them. These will be output to a file with a void function header. Date that do not exist will get output to a different file with the same header. I can get it to output the header and the first date, however it stops there and will not read anymore dates. I'll just include the important part with rough documentation.
I'm new to this and any help would be great.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  // prompt user to enter an input file name
	cout << "Please enter the file name that contains the date; press enter." << endl;
	cin >> fileName;
	//associate input file variable with the user input file 
	din.open(fileName);
	
	//test status of of the input file variable
	if (!din)
	{ // the input file was not found
		//output error message
		cout << "Input file " << fileName << " was not found." << endl;
		cout << "Please re-run program" << endl;
	}
	else
	{ // input file was found
		
		//initialize LCV
		leapYear = true;
		din >> month >> day >> year; // read in month, day, year
		
		//while more data in file
		while (din)
		{
			//call SVRF function to determine name of month
			monthName = MonthToString(month);

			// call SVRF to determine if month is valid
			validMonth = ValidMonth(month);

			//call SVRF to determine is day is valid
			validDay = ValidDay(month, day, leapYear);

			//call SVRF function to determine day
			dayOfYear = CalcDays(month, day);

			//call SVRF funtion to determine leap year
			leapYear = IsLeap(year);



             // use if statement to add an extra day if the year is a leap year
			if (leapYear == true && month > 2)
			{
				dayOfYear++;  
                      // adds one day to the day of year if year is a leap year
			}



			if (validDay != true || validMonth != true)
			{
				dout.open("error.txt");
				PrintHeading(dout, fileName);

				if (validDay != true)
				{
			             dout << "The date" << month << day << year
						<< " is invalid because the day is invalid." << endl;
				}
				else if (validMonth != true)
				{
					dout << "The date" << month << day << year
						<< " is invalid because the month is invalid." << endl;
				}

			}
			else
			{
				dout.open("results.txt");
			    PrintHeading(dout, fileName);

				dout << monthName << " " << day << " is day number " << dayOfYear
					<< " in the year " << year << "." << endl;

			}

			// modify LCV - Get another date
			din >> month >> day >> year;

		}

		din.close();
		dout.close();
	}

	return 0;
The only reason (apart from a crash somewhere else in the code) why it stops is that din detects an error. I.e. the data is not as expected.

By the way: You can remove line 19/77 and put it in while(din >> month >> day >> year)
The data in the file is simply just MM DD YYYY going down the file. i.e.
1 2 2000
13 14 2015
2 6 2004

It does send the invalid date to the correct file with the header, but again it only does it once.
Also, the teachers asked we put the din >> month >> day >> year at the bottom to show we understand the basic structure of it. Just for showing them we can I guess.

I'll print the rest of the program and maybe someone will see something?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Author: Stephen Lester
Date: 04.15.2015
Purpose: To calculate the number day of the year, based on month and whether
		it is a leap year or not
Assumptions: -the data file will always be in the correct format
			- the dates will be in the correct format
			- all dates exist
Exception Handling: If data file is invalid or can't be found, outputs message
*/

#include<iostream>			// allows i/o
#include<string>			// allows string datatype
#include<fstream>			// allows file i/o

using namespace std;


//declare constants
int JAN = 31;
int FEB = 28;
int MARCH = 31;
int APRIL = 30;
int MAY = 31;
int JUNE = 30;
int JULY = 31;
int AUG = 31;
int SEPT = 30;
int OCT = 31;
int NOV = 30;
int DEC = 31;


// function prototypes
string MonthToString(int numMonth);	// will calculate month name based on month number
bool IsLeap(int yearNum);				// will determine if year is leap year
int CalcDays(int numMonth, int date); // calculates number day of year
void PrintHeading(ofstream& dout, string fileName);
bool ValidMonth(int month);
bool ValidDay(int month, int day, bool leapYear);

int main()
{
	// variables
	int month;			//month from file
	int day;			// day from file
	int year;			// year from file
	string fileName;	// file name to be input by user
	ifstream din;		//input file variable
	ofstream dout;      // output file variable
	string monthName;	// varibale to store the month's name after the function determines it
	int dayOfYear;		// variable to store the day of the year
	bool leapYear;		// variable to store truth value of whether it is a leap year
	bool validDay;
	bool validMonth;

	// prompt user to enter an input file name
	cout << "Please enter the file name that contains the date; press enter." << endl;
	cin >> fileName;
	//associate input file variable with the user input file 
	din.open(fileName);
	
	//test status of of the input file variable
	if (!din)
	{ // the input file was not found
		//output error message
		cout << "Input file " << fileName << " was not found." << endl;
		cout << "Please re-run program" << endl;
	}
	else
	{ // input file was found
		
		//initialize LCV
		leapYear = true;
		din >> month >> day >> year; // read in month, day, year
		
		//while more data in file
		while (din)
		{
			//call SVRF function to determine name of month
			monthName = MonthToString(month);

			// call SVRF to determine if month is valid
			validMonth = ValidMonth(month);

			//call SVRF to determine is day is valid
			validDay = ValidDay(month, day, leapYear);

			//call SVRF function to determine day
			dayOfYear = CalcDays(month, day);

			//call SVRF funtion to determine leap year
			leapYear = IsLeap(year);



			// use if statement to add an extra day if the year is a leap year
			if (leapYear == true && month > 2)
			{
				dayOfYear++;  // adds one day to the day of year if year is a leap year
			}



			if (validDay != true || validMonth != true)
			{
				dout.open("error.txt");
				PrintHeading(dout, fileName);

				if (validDay != true)
				{
					dout << "The date" << month << day << year
						<< " is invalid because the day is invalid." << endl;
				}
				else if (validMonth != true)
				{
					dout << "The date" << month << " " <<  " " << day << " " << year
						<< " is invalid because the month is invalid." << endl;
				}

			}
			else
			{
				dout.open("results.txt");
			    PrintHeading(dout, fileName);

				dout << monthName << " " << day << " is day number " << dayOfYear
					<< " in the year " << year << "." << endl;

			}

			
			// modify LCV - Get another date
			din >> month >> day >> year;

		}

	}
	
	din.close();
	dout.close();


	return 0;
}


And the functions are:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
string MonthToString(int numMonth)
{
	/*
	Pre: variable numMonth must be defined
	Post: the month name will be calculated and returned
	Purpose: determine the month based on input number
	*/

	//declare variable[s]
	string monthName;

	// determine month's name based on entered month's digit
	if (numMonth == 1)
	{
		monthName = "January";  //store january as month's name
	}
	else if (numMonth == 2)
	{
		monthName = "February"; // store february as month's name
	}
	else if (numMonth == 3)
	{
		monthName = "March"; // store march as month's name
	}
	else if (numMonth == 4)
	{
		monthName = "April"; //store april as month's name
	}
	else if (numMonth == 5)
	{
		monthName = "May"; //store may as month's name
	}
	else if (numMonth == 6)
	{
		monthName = "June"; //store june as months's name
	}
	else if (numMonth == 7)
	{
		monthName = "July"; //store july as month's name
	}
	else if (numMonth == 8)
	{
		monthName = "August"; //store august as month's name
	}
	else if (numMonth == 9)
	{
		monthName = "September"; //store september as month's name
	}
	else if (numMonth == 10)
	{
		monthName = "October"; // store october as month;s name
	}
	else if (numMonth == 11)
	{
		monthName = "November"; // store november as month's name
	}
	else				// use just "else" because every other option is taken, 
	{					// assuming only valid months are entered
		monthName = "December"; // store december as month's name
	}



	return monthName;	// return the name of the month
}



int CalcDays(int numMonth, int date)
{
	/*
	Pre: variables numMonth and year must be defined
	Post: day number of the year will be calculated and returned
	Purpose: calculate day number in the year
	*/

	//declare vriable[s]
	int dayOfYear;

	//determine the day based on the day and month
	if (numMonth == 1)
	{
		dayOfYear = date;  
		//for january, the day of the year is just the date entered
	}
	else if (numMonth == 2)
	{
		dayOfYear = date + JAN; 
		//february, the day is equal to the date plus the days from january
	}
	else if (numMonth == 3)
	{
		dayOfYear = date + JAN + FEB; 
		// march, the day is equal to the date plus the days
	}	// from jan and feb
	else if (numMonth == 4)
	{
		dayOfYear = date + JAN + FEB + MARCH; 
		//april, day is equal to the date plus the days
	}	// from jan, feb, march
	else if (numMonth == 5)
	{
		dayOfYear = date + JAN + FEB + MARCH + APRIL; 
		//may, day is equal to the date plus the
	}   // days from jan, feb, march...
	else if (numMonth == 6)
	{
		dayOfYear = date + JAN + FEB + MARCH + APRIL + MAY; 
		//june, day is equal to the date plus
	}	// plus the days from all preceeding months
	else if (numMonth == 7)
	{
		dayOfYear = date + JAN + FEB + MARCH + APRIL + MAY + JUNE;
		// july, day is equal to the date 
	}	//  plus the days from all preceeding months
	else if (numMonth == 8)
	{
		dayOfYear = date + JAN + FEB + MARCH + APRIL + MAY + JUNE; 
		// august, day is equal to thE 
	}	// date plusthe days from all preceeding months
	else if (numMonth == 9)
	{
		dayOfYear = date + JAN + FEB + MARCH + APRIL + MAY + JUNE + JULY + AUG; 
		// september, day is equal to
	}	//the date plus all preceeding months
	else if (numMonth == 10)
	{
		dayOfYear = date + JAN + FEB + MARCH + APRIL + MAY + JUNE + JULY + AUG + SEPT;	
		//october, the day is equal
	}	// to the date plus all preceeding months
	else if (numMonth == 11)
	{
		dayOfYear = date + JAN + FEB + MARCH + APRIL + MAY + JUNE + JULY + AUG 
			+ SEPT + OCT; 
		// november
	}	// the date is equal to the date plus all preceeding months		
	else
	{
		dayOfYear = date + JAN + FEB + MARCH + APRIL + MAY + JUNE + JULY + AUG
			+ SEPT + OCT + NOV;
		//december
	}  //the date is equal to the date plus all preceeding months



	return dayOfYear;	//returns the day of the year to main
}



bool IsLeap(int yearNum)
{
	/*
	Pre: variable yearNum must be defined
	Post: the truth of whether it is a leap year will be determined and returned
	Purpose: calculate if year is a leap year
	*/

	//declare variable[s]
	bool leapYear;

	// if the year is divisble by 4, 100, or 400
	if (yearNum % 4 == 0 || yearNum % 100 == 0 && yearNum % 400 == 0)
	{   // it is true it is a leap year
		leapYear = true;
	}
	else
	{// if it is not, it is false it is a leap year
		leapYear = false;
	}


	return leapYear;	//return true or false
}


void PrintHeading(ofstream& dout, string fileName)
{

	dout << "*********************************" << endl;
	dout << "*                               *" << endl;
	dout << "*   Days Calculation Program    *" << endl;
	dout << "*     File name: " << fileName << "      *" << endl;
	dout << "*                               *" << endl;
	dout << "*********************************" << endl;


}


bool ValidMonth(int month)
{
	bool validMonth;    // variable to store is month is valid

	if (month > 12 || month < 1)
	{
		validMonth = false;
	}
	else
	{
		validMonth = true;
	}


	return validMonth;
}


bool ValidDay(int month,int day, bool leapYear)
{
	bool validDay;

	if (month == 9, 4, 6, 11 && day > 30)
	{
		validDay = false;
	}
	else if (month == 2 && day > 28 && leapYear == false)
	{
		validDay = false;
	}
	else if (month == 1, 3, 5, 7, 8, 10, 12 && day > 31)
	{
		validDay = false;
	}
	else
	{
		validDay = true;
	}

	return validDay;
}
I suggest to place some debug cout if you're not sure whether it runs or not.

Your while(din) works as intended.

The problem is dout.open("error.txt"); and dout.open("results.txt");. You cannot open a stream twice. Move ofstream dout; before both open and use the flag app to add the data. Otherwise it will be overwritten.

See:
http://www.cplusplus.com/reference/fstream/fstream/open/
Topic archived. No new replies allowed.