Getting an extra result

I can't for the life of me figure out what I'm doing wrong. I have four dates that are being read in.

01 01 2011 5
04 04 2010 10
11 23 2009 200
03 01 2004 29

I've asked the program to print out the ordinal form of these dates which is the number of days from the beginning of the year. I'm getting the correct results but I'm getting one extra one.

My results are:

1
94
327
61
91

Any help would be greatly appreciated.


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
#include <iostream>
#include "datefuns.cc"
#include <fstream>
#include <iomanip>

using namespace std;

/********************************Date to Long Date Function *******************************************************/





/*********************************Convert Date to Ordinal Form Function********************************************/

// This program will take in a date in mm dd yyyy form and convert it to an ordinal date

int ordinalForm (int month, int day, int year)
{

	int ordinalDay1;                   // Declares the ordinal date integer

	// Calculate the number of days for each month without taking into consideratoin the
	//   leap year
	
	if (month == 1)                             // This calculates days in January
		ordinalDay1 = day;
	else if (month == 2)                     // This calculates days in February
		 ordinalDay1= 31 + day;
	else if (month == 3)                     // This calculates days in March
		ordinalDay1 = 59 + day;
	else if (month == 4)                     // This calculates days in April
		ordinalDay1 = 90 + day;
	else if (month == 5)                     // This calculates days in May
		ordinalDay1 = 120 + day;
	else if (month == 6)                     // This calculates days in June
		ordinalDay1 = 151 + day;
	else if (month == 7)                     // This calculates days in July
		ordinalDay1 = 181 + day;
	else if (month == 8)                       // This calculates days in August
		ordinalDay1 = 212 + day;
	else if (month == 9)                       // This calculates days in September
		ordinalDay1 = 243 + day;
	else if (month == 10)                     // This calculates days in October
		ordinalDay1 = 273 + day;
	else if (month == 11)                     // This calculates days in November
		ordinalDay1 = 304 + day;
	else                                                // This calculates days in December
		ordinalDay1 = 334 + day;
	
	// If it is a leap year then if the date is after February 28th, you would add this
	

	if (isLeapYear (year) && (month >= 3))    // This is to say that if it is a leap year, then on or after
		ordinalDay1 = ordinalDay1 +1;                // the 3rd month, you add 1 day to the day of the year
	
	// Return the ordinal date to the main function
	
	return (ordinalDay1);
	
}



/*****************************************Main Function**********************************************************/
int main ()
{

	ifstream inFile;                     // Declaration of file that holds all the information
	int month;                           // This is the month of the year
	int day;                             // The day of the month
	int year1;                            // This is the year
	int numberDaysChange;                // This is the number of days that the original date will change
	string currentLine;                  // The current line that is being read in
	int ordinalDay1;                     // This is the current date in ordinal form
	int ordinalDay2;                     // The second ordinal day after the number of days changed
	int currentYear;                    // The length of the current year
	int year2;                          // The year after the change in number of days

	// Open up the file and test for a good open

	inFile.open ("dates.txt");

	if (!inFile)
	{
		cout << "File did not open correctly. Exiting program..." << endl;
		return 1;
	}
	
	
	// Begin the while loop to read in the date and display it in the new format
	
	while (inFile)
	{
		
		// Read in the month, day, year, and the number of days to add or subtract
		
		inFile >> month >> day >> year1 >> numberDaysChange;
		
		// Calculate the length of the current year
		
		if (isLeapYear (year1))
			currentYear = 366;
		else
			currentYear = 365;
		
		// Call the function to convert the date to ordinal form
		
		ordinalDay1 = ordinalForm (month, day, year1);
		
		// Add numberDaysChange to the ordinal form number
		
		ordinalDay2 = (numberDaysChange + ordinalDay1);
		
		// Use if else statement to determine if the ordinal date forces a new year
		
		if (ordinalDay2 > currentYear)
		{
			ordinalDay2 -= currentYear;
			year2 = year1 + 1;
		}
			else if (0 < currentYear)
			 ;	
			else if (ordinalDay2 < 0)
				
				year2 = year1 -1;
			
		
		
		// Call function in datefuns.cc to convert back to mm dd yyyy form
		
		dayToDate (ordinalDay2, year2, month, day);
		
		cout << ordinalDay1 << endl;
		
	}
	
	return 0;
	
}
Try changing
while (inFile)
to
while (inFile.good())
01 01 2011 5
04 04 2010 10
11 23 2009 200
03 01 2004 29

What is the 5, 10 , 200, 29?
Those are different numbers that will change the date to a different ordinal form. They aren't affecting this I don't think though because I'm getting the right numbers out, I'm just getting one extra one.
binarybob350: That fixed the problem. I really appreciate it. Why does it work like that though? I thought what I had in there was correct.
Topic archived. No new replies allowed.