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

How about this one? It's also in the C code snippets. I never knew it would work for anything below 1900, but it shows 01/01/1800 to be a Wednesday.

// function to return the day of the week given the date
// Original Turbo C modified for Pelles C

#include <stdio.h>      // for printf(), scanf(), getchar()

int weekday(int month,int day,int year);

char week[7][10] = {
	   "Monday","Tuesday","Wednesday","Thursday",
           "Friday","Saturday","Sunday"
     };

int main(void)
{
  int  month, day, year;
  
  printf("Return the day of the week given the date ...\n");
  printf("\nEnter date in the form mm/dd/yyyy : ");
  scanf("%d/%d/%d",&month,&day,&year);
  printf("\nThe day of the week for this date is %s\n\n",week[weekday(month,day,year)]);
  
  getchar();   // wait 
  getchar();   // 2nd wait needed
  return 0;
}

//
// given month, day, year, returns day of week, eg. Monday = 0 etc.
// good for 1901 to 2099
// 
int weekday(int month,int day,int year)
{	
  char ix, tx, vx;

  switch (month) {
    case 2  :
	case 6  : vx = 0; break;
	case 8  : vx = 4; break;
	case 10 : vx = 8; break;
	case 9  :
	case 12 : vx = 12; break;
	case 3  :
	case 11 : vx = 16; break;
	case 1  :
	case 5  : vx = 20; break;
	case 4  :
	case 7  : vx = 24; break;
  }
  if (year > 1900)
    year -= 1900;
  ix = ((year - 21) % 28) + vx + (month > 2);  // take care of February 
  tx = (ix + (ix / 4)) % 7 + day;              // take care of leap year
  return (tx % 7);
}

Numbers of days between 1/1/1800 and 21/11/2004 is 74836...check it please..

Sorry everyone ...i just found out that leap year is a year which is not only divisible by 4 but also
years divisible by 100 are NOT!
Unless they are also divisible by 400
i never knew about the last 2 points, so now i corrected my program..which gives the result 1/1/1800 as wednesday... Scott, vegaseat's code mighy have helped you....

i will try my best to understand you how to find day of week...
First set a date whose day of week you know..
for instance (21/11/2004 is sunday) then find the no. of days between this date and the date which user entered.then take mod 7..and if its return comes out to be 0 then its sunday else if it is 1 then its monday else if its 2 its tuesday and so on... thats how i did..i get the calendar from the program..

The number of days from 01/01/1800 to 11/21/2004 is 74833. I used this little code below. Interesting as far as algorithms are concerned, but the span needs to be more than a year to trap leap years correctly.

// Find the number of days between given dates mm/dd/yyyy
// algorithm does not work properly with leap years, if the
// span is less than a year
// right number for 01/01/2004 to 01/01/2005 (366 days)
// wrong number for 01/01/2004 to 12/31/2004 (364 days)
// right number for 01/01/1900 to 01/01/2000 (36524 days)
// right number for 01/01/2000 to 01/01/2100 (36525 days)
// since 2000 was a leap year and 1900 was not 
// (years ending with 00 have to be divisible by 400 to leap)
// Code modified to compile with Pelles C (dns)

#include <stdio.h>   // printf(), scanf()

long days(int sm, int sd, int sy, int em, int ed, int ey);
long factor(int mm, int dd, int yy);

int main(void)
{
  int  sm, sd, sy;    // starting date
  int  em, ed ,ey;
  long old;

	printf("\n  Calculate days between two dates ...");
	printf("\n\n  Enter starting date in mm/dd/yyyy format: ");
	scanf("%d/%d/%d",&sm,&sd,&sy);
	printf("\n  Enter ending date in mm/dd/yyyy format:   ");
	scanf("%d/%d/%d",&em,&ed,&ey);
	old = days(sm,sd,sy,em,ed,ey);
	
	printf("\n  There are %ld days between %d/%d/%d and %d/%d/%d\n",
			old,sm,sd,sy,em,ed,ey);

  getchar();   // wait 
  getchar();   // 2nd wait needed
	return 0;
}

//
// returns number of days between start and end dates
// span has to be more than a year to catch leap years properly
//
long days(int sm, int sd, int sy, int em, int ed, int ey)
{
  long fac1, fac2, elap;

  fac1 = factor(sm, sd, sy);
	fac2 = factor(em, ed, ey);
  elap = fac2 - fac1;
  return (elap);
}


long factor(int mm, int dd, int yy)
{
  long xx;

  xx = 365 * yy + dd + 31 * (mm - 1);
	if (mm < 3)
		xx = xx + ((yy -1)/4) - (.75 * ((yy - 1)/100)+1);
	else
		xx = xx - (.4 * mm + 2.3) + (yy / 4) - (((.75 * (yy / 100) + 1)));
	return (xx);
}
This question has already been answered. Start a new discussion instead.