Problem with converting ordinal date to mm dd yyyy date form

I am writing a program as a hw project and I am having problems with this last little bit of it. The idea of the program is to take a date in the form of
mm dd yyyy. It is then supposed to convert that date to an ordinal date and then add a number to that date and then convert it back to mm dd yyyy form. When it prints out the information to the screen it is supposed to say something like "8 days from Sunday, December 25, 2011 is Monday, January 2, 2012. The program to output the day of the week is supplied by my teacher. I am having problems with it spitting out the same date in the above format twice. Any help would be greatly appreciated. I know the problem lies in my Function 2 portion, I just cant figure out what I have wrong.

#include <iostream>
#include <fstream>
#include "datefuns.cc"
#include <cmath>
#include <string>

using namespace std;

// Function 1
// Converts the date in the form of mm dd yyyy to the ordinal day of the year

int OrdinalDate ( int month, int day, int year )

{

int days; // Total days counted
int numDays; // Used as a variable for when February has 28 or 29 days

/* Equation used to input into isLeapYear function above
to then assign a value to numDays */
if ( isLeapYear ( year ) )
numDays = 29;
else
numDays = 28;

// Calculate the amount of days

if ( month == 01 )
days = day;

else if ( month == 02 )
days = day + 31;

else if ( month == 03 )
days = day + 31 + numDays;

else if ( month == 04 )
days = day + 31 + numDays + 31;

else if ( month == 05 )
days = day + 31 + numDays + 31 + 30;

else if ( month == 06 )
days = day + 31 + numDays + 31 + 30 + 31;

else if ( month == 07 )
days = day + 31 + numDays + 31 + 30 + 31 + 30;

else if ( month == 8 )
days = day + 31 + numDays + 31 + 30 + 31 + 30 + 31;

else if ( month == 9 )
days = day + 31 + numDays + 31 + 30 + 31 + 30 + 31 + 31;

else if ( month == 10 )
days = day + 31 + numDays + 31 + 30 + 31 + 30 + 31 + 31 + 30;

else if ( month == 11 )
days = day + 31 + numDays + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;

else if ( month == 12 )
days = day + 31 + numDays + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;

return days;
}

// Function 2
// Converts from ordinal form to month, day, year, form

string convertDate ( int ordinalDate, int& year, int& month, int& day )
{

// Compute day name
int num = dow (year, month, day);
string daysName = dayOfWeekToDayName ( num );

// Compute month name
string monthsName = monthNumToMonthName( month );

// Put the pieces together and return date in long form
return daysName + ", " + monthsName + " " + intToString( day )
+ ", " + intToString ( year );

}

// Function 3
// Compute to the long form of a date from it's month, day, year form

string ComputeLongForm ( int month, int day, int year )
{

// Compute day name
int num = dow (year, month, day);
string dayName = dayOfWeekToDayName ( num );

// Compute month name
string monthName = monthNumToMonthName( month );

// Put the pieces together and return date in long form
return dayName + ", " + monthName + " " + intToString( day )
+ ", " + intToString ( year );
}


int main ()
{
ifstream inData; // Opens text file
string fileName; // Value for stating file did not open properly
string longform; // Form of the new date
string originalDate;
int month1; // Month being read in from text file
int day1; // Day being read in from text file
int year1; // Year being read in from text file
int N; // Integer that is the number of days to add or subtract
int ordinaldate; /* Ordinal date of the year having days added to
or subtracted from */

// Open text file
inData.open ( "dates.txt" );
if (!inData)
{
cout << fileName << " was not found. Ending program!";
return 1;
}

// Begin reading the first line
inData >> month1 >> day1 >> year1 >> N;

// Begin loop
while ( inData )
{
// Call the ordinal date function ( Function 1)
ordinaldate = OrdinalDate ( month1, day1, year1 );

// Adjust the ordinal date by "N"
ordinaldate = ordinaldate + N;

// Figuring out what year it is
if ( ordinaldate >= 365 )
{
if (isLeapYear ( year1 )) // Used if the original year goes up one year
{
ordinaldate = ordinaldate - 366;
year1++;
}
else
{
ordinaldate = ordinaldate - 365;
year1++;

}

}
else if ( ordinaldate < 0 ) // Used for when original year goes down by one year
{
if (isLeapYear ( year1 ))
{
ordinaldate = ordinaldate + 366;
year1--;
}
else
{
ordinaldate = ordinaldate + 365;
year1--;
}
}


// Convert the ordinal date back into month, day, year format ( Function 2)
originalDate = convertDate ( ordinaldate, year1, month1, day1 );

// Convert both dates to the long format ( Function 3)
longform = ComputeLongForm ( month1, day1, year1 );

// Read out the final listing of the dates
cout << N << " days from " << originalDate << " is "
<< longform << endl;

// Grab the next line in the file
inData >> month1 >> day1 >> year1 >> N;
}


// Close the opened text file and exit the program
inData.close ();

return 0;

}
Topic archived. No new replies allowed.