*sigh* whole code isn't working

So this is math based on http://www.jimloy.com/math/day-week.htm My assignment is due today, and I am frustrated and just want the code to work. I don't really want to scrap all of code and try another algorithm, because I've been working for days on this, but I really need help. It's a long code, but it's not necessarily looking for shortcuts, but for a properly working code. Please help if you can.

It's the last function that's the main, larger function(calender function)... The calender function is supposed to take a day, month, and year and tell what day of the week it is... as it works right now, 2/20/2012 says it's a Wednesday, but as you know, it's Monday :/


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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*************************************
Leap Year function returns true/false if test is leap year/is not leap year, respectively - called by calendar function
*************************************/
bool leapYear(int test)
{
	int remainder;

	remainder = test%4;

	if(remainder==0)
	{
		if (test%100 == 0)
		{
			remainder = test%400;
			if (remainder==0)
				return true;
			else
				return false;
		}
		else
			return true;
	}
	else
		return false;	
}// leapYear function
/*************************************
century function calculates what century a year is to apply it to the math in calendar function
*************************************/
int century(int gregorianyear)
{
	int temp;
	gregorianyear = gregorianyear/100;
	temp = gregorianyear;

top:
		if(temp==19)
			return 0;
		else if(temp==20)
			return 5;
		else if(temp==21)
			return 4;
		else if(temp==22)
			return 2;
		else 
		{
			temp = temp - 4;
			goto top;
		}
}//century
/************************************
leapdays function returns the number of leapdays as needed for calender function
***********************************/
int leapdays(int year, int month)
{
	int temporary,counter, loop;
	counter = 0;
	

	for(loop=25; loop > 0; loop--)
	{
		temporary = year%4;
		if (temporary==0)
		{
			year = year/4;
			counter = counter+1;
			if (year==0)
				break;
		}
		else
			year= year-1;
	}
	if (temporary == 0)
	{
		if(month==1 || month==2)
			counter = counter-1;
	}
	
	return counter;
}//leapdays
			

	
/****************************************
getmonth function takes month to convert it for math in calendar
*****************************************/
int getmonth(int inputmonth)
{
	//originally I had less statements with || separating those with similar
	//returns, but the logic only worked with 12 separate statements.
	if (inputmonth == 1)
		return 0;
	else if (inputmonth == 2)
		return 3;
	else if (inputmonth ==3)
		return 3;
	else if (inputmonth == 4)
		return 6;
	else if (inputmonth == 5)
		return 1;
	else if (inputmonth == 6)
		return 4;
	else if (inputmonth ==7)
		return 6;
	else if (inputmonth == 8)
		return 2;
	else if (inputmonth == 9)
		return 5;
	else if (inputmonth == 10)
		return 0;
	else if (inputmonth == 11)
		return 3;
	else if (inputmonth == 12)
		return 5;
}//getmonth


/*************************************
calendar function takes a month,day,year to calculate the day of year - calls leapyear and century functions
*************************************/
void calendar ()
{
	int month,day,year, lasttwo, temp,x;
	int total;
	bool leap = false;
	cout << "LAST FUNCTION, TELLS WHAT DAY OF WEEK A PARTICULAR DAY IN A YEAR IS." << endl;
	cout << "What month? (1-12)";
		cin >> month;
	cout << endl;
	cout << "What day? (1-31)";
		cin >> day;
	cout << endl;
	cout << "What year? (>=1901)";
		cin >> year;
	cout << endl;

	
	if (leapYear(year))
	{
		cout << year << " is a leap year." << endl;
	}
	else 
	{
		cout << year << " is not a leap year." << endl;
	}

	total = (century(year)+ year%100 + leapdays(year,month)+ getmonth(month)+ day)%7;

	
	if (total==0)
		cout << "It was a Sunday on " << month << "-" << day << "-" << year << endl;
	else if (total==1)
		cout << "It was a Monday on " << month << "-" << day << "-" << year << endl;
	else if (total==2)
		cout << "It was a Tuesday on " << month << "-" << day << "-" << year << endl;
	else if (total==3)
		cout << "It was a Wednesday on " << month << "-" << day << "-" << year << endl;
	else if (total==4)
		cout << "It was a Thursday on " << month << "-" << day << "-" << year << endl;
	else if (total==5)
		cout << "It was a Friday on " << month << "-" << day << "-" << year << endl;
	else if (total==6)
		cout << "It was a Saturday on " << month << "-" << day << "-" << year << endl;

}//calender function 
Last edited on
If you just test calendar() and you notice something is wrong it can be hard to know where the bug is.

It's much easier if you first make sure that leapYear, century, leapdays and getmonth works correctly by doing some test. When you are sure all of them work correctly you go on testing calendar().
Last edited on
Well, I'm almost positive posting to the forums helps me, even without getting any answers haha. Decided to go with a more simple code. Here's my fix(I used Zeller's Congruance theory rather than the shady who-ever's 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*************************************
Leap Year function returns true/false if test is leap year/is not leap year, respectively - called by calendar function
*************************************/
bool leapYear(int test)
{
	int remainder;

	remainder = test%4;

	if(remainder==0)
	{
		if (test%100 == 0)
		{
			remainder = test%400;
			if (remainder==0)
				return true;
			else
				return false;
		}
		else
			return true;
	}
	else
		return false;	
}// leapYear function


/***********************************************************************************************
calendar function takes a month,day,year to calculate the day of year - calls leapyear function
************************************************************************************************/
void calendar ()
{
	int month,day,year;
	int total;
	bool leap = false;
	cout << "LAST FUNCTION, TELLS WHAT DAY OF WEEK A PARTICULAR DAY IN A YEAR IS." << endl;
	cout << "What month? (1-12)";
		cin >> month;
	cout << endl;
	cout << "What day? (1-31)";
		cin >> day;
	cout << endl;
	cout << "What year? (>=1901)";
		cin >> year;
	cout << endl;

	
	if (leapYear(year))
	{
		cout << year << " is a leap year." << endl;
	}
	else 
	{
		cout << year << " is not a leap year." << endl;
	}

	if (month+1 <= 3)
	{
		month = month+12;
		year = year-1;
	}

	total = (day + (((month+1)*26)/10)+ year + (year/4)+ (6*(year/100))+ (year/400))%7;

	if (month >= 13)
	{
		month = month-12;
		year = year+1;
	}



	
	if (total==0)
		cout << "It was a Saturday on " << month << "-" << day << "-" << year << endl;
	else if (total==1)
		cout << "It was a Sunday on " << month << "-" << day << "-" << year << endl;
	else if (total==2)
		cout << "It was a Monday on " << month << "-" << day << "-" << year << endl;
	else if (total==3)
		cout << "It was a Tuesday on " << month << "-" << day << "-" << year << endl;
	else if (total==4)
		cout << "It was a Wednesday on " << month << "-" << day << "-" << year << endl;
	else if (total==5)
		cout << "It was a Thursday on " << month << "-" << day << "-" << year << endl;
	else if (total==6)
		cout << "It was a Friday on " << month << "-" << day << "-" << year << endl;
}//calendar funtion 
Often scapping and starting again can be much faster than fixing existing code...

FYI, your leapdays function was wrong in your first code, not sure how exactly since I'm not going to work out what it should be but that's where the error was.

7/20/1969 should give 17 for it but it gives 6.
Topic archived. No new replies allowed.