Thank you for the reply Disch, that seemed to have fixed that issue. Now I am stuck on some really weird issues with the number of days that doesn't seem to be following any particular pattern.
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
|
int computeOffset(int month, int year)
{
int days = 0;
int offset = 0;
int totalDays = 0;
int totalDaysMonths = 0;
int totalDaysYears = 0;
int daysYear = 0;
for (int a = 1753; a < year; a++)
{
if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0))
daysYear = 366;
else
daysYear = 365;
totalDaysYears += daysYear
}
for(int b = 1; b < month; b++)
{
if (b == 1 || b == 3 || b == 5 || b == 7 || b == 8 || b == 10 || b == 12)
days = 31;
else if(b == 4 || b == 6 || b == 9 || b == 11)
days = 30;
else if(b == 2)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
days = 29;
else
days = 28;
}
totalDaysMonths += days;
}
offset = ((totalDaysYear + totalDaysMonths) % 7);
return offset;
}
|
I'm boggled with this bug I'm getting. There are occasions (maybe just dumb luck?) in which my calendar loads perfectly with the code I have to print it to the screen, and then there are other times when the calendar is completely messed up.
For instance, with my code, the program produces a calendar for June 2008. I am running a piece of code with the computeOffset function to display the total amount of days from the last day of the previous month (so May 31st), to Jan 1st, 1753.
My result is 92388 days. Using a website to do the same calculation, I get the same exact number. I can repeat the process with April 1820, and Feb 1900 (these are dates used by our testing software). So I'm doing something correct here.
BUT, for instance, December 2050, my code produces 108,813 days, where it should be 108,811 days. Same issue on November 2002, I get 91251 days from my code, but the correct answer is 91249. It seems there is an off-by-two issue here.
I always here it's good to have a second pair of eyes on this stuff, especially when you've been staring at it for so long. Does anyone have any insight?