Hi,
This is really a logic problem more so than a stright up C++ problem.
For class I have written a program that takes dates from a text file and then finds the number of days from January 1st 1800 that days is. The program also has to be able to take the number (that number being the number of days since 1800 that date is) and convert it back into a date (in other words finding the month, day, year and day of the week). What makes this quite a bit trickier is taking into account leap years and the effect they have. I can't seem to figure out how to find the date from the number of days. If you want to look at it below is the function that I wrote that finds the number of from January 1st a date is:
int DaysFrom1800::numDaysFrom1800 (int day, int month, int year) {
// Declare leapCount variable to keep track of number of leap years and days variable
// to store number of days
int leapCount =0;
int days = 0;
// For loop to find the number of leap years
for (int i=0; i < year; i++) {
if (leapCheck(i))
leapCount ++;
}
// Check to see if current year is a leap year
bool thisYear = leapCheck(year);
//Find the Number of Days From Complete Years
days = ((year-1800-leapCount)*365) + (leapCount*366);
// Find the Number of Days From the Months
switch (month) {
case 1:
days = days + day;
break;
case 2:
if (day < 28)
days = days + 31 + day;
if (thisYear)
days = days + 31 + 29;
break;
case 3:
if (thisYear)
days = days +31+29+ day;
else
days = days + 31 + 29 + day;
break;
case 4:
if (thisYear)
days = days + 31 + 29 + 31 + day;
else
days = days + 31 + 28 + 31 + day;
break;
case 5:
if (thisYear)
days = days + 31 + 29 + 31 + 30 + day;
else
days = days +31 + 28 + 31 + 30 + day;
break;
case 6:
if (thisYear)
days = days +31 + 29 + 31 + 30 + 31 + day;
else
days = days + 31 + 28 + 31 + 29 + 31 + day;
break;
case 7:
if (thisYear)
days = days + 31 + 29 + 31 + 30 + 31 + 30 + day;
else
days = days + 31 + 28 + 31 + 30 + 31 + 30 + day;
break;
case 8:
if (thisYear)
days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + day;
else
days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
break;
case 9:
if (thisYear)
days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + day;
else
days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + day;
break;
case 10:
if (thisYear)
days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day;
else
days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day;
break;
case 11:
if (thisYear)
days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + day;
else
days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + day;
break;
case 12:
if (thisYear)
days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day;
else
days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day;
break;
}
return days;
}
If somone could help me in figuring out the logic of how to convert the day number back to a date (and find the day of the week, we do know that January 1st 1800 is a Wednesday) that would be awesome.
thanks,
-Scott