Yet another c++ calendar thread..

Hi everyone, I'm taking a beginning C++ class, and we're working on a project to write a calendar using loops. I'm nearly done with my project, but I just realized I have two major bugs that I'm struggling with in my code.

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
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; year++)
	{
		if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0))
			daysYear = 366;
		else
			daysYear = 365;
		totalDaysYears += daysYear  // this is giving a HUGE number in the billions for some reason
	}

	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; //My Leap Year calculation is always giving 29 days on February for some reason
	}

	offset = ((totalDaysYear + totalDaysMonths) % 7);
	return offset;
}


I had to rewrite that code by copying it from my program (we use a remote connection to access linux computers on campus, and I don't know how to copy-paste from emacs.)

Anyway, my major bugs were included in comments. I know for a fact my months other than February are calculating as intended. But I can't figure out why my February is always putting out an extra day (off-by-one error).

And then my years is coming out with a crazy number that is way out there. Specifically: 2,146,753,648 (That number looks really familiar, is it a power of two?)

I think I'm close to figuring my leap year issue out, but my calculation on years is driving me nuts!
re: your year problem...

in your for loop on line 10, you are incrementing your year... and not your loop counter (a). You probably meant to be incrementing 'a' instead. As it is written now... that loop will not end until 'year' exceeds the maximum capacity for an int and wraps around to a negative number.

This is probably the problem with your leap year calculation on line 27 as well.. since by that point the contents of 'year' has been destroyed by that first loop.
Last edited on
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?
Topic archived. No new replies allowed.