Calender issues

So i am creating a calendar in c++ for school and ive got the code down for the most part but when it tries to output a calendar for December 1753 it comes out completely wrong.

the calendar starts January 1753 and goes from there

here is my code:

#include <iostream>
#include<iomanip>
using namespace std;


int numDays = 0;
int offset = 0 ;

int getMonth()
{
int month = 0;
do
{
cout << "Enter a month number: ";
cin >> month;

if (month < 1 || month > 12)
cout << "Month must be between 1 and 12.\n";
}
while (month < 1 || month > 12);


return month;
}

int getYear()
{
int year = 0;
do
{
cout << "Enter year: ";
cin >> year;

if (year < 1753)
cout << "Year must be 1753 or later.\n";
}
while (year < 1753);

return year;
}

bool isLeapYear(int year)
{
bool leapYear = false;

if (year % 4 == 0)
leapYear = true;
if (year % 100 == 0 && year % 400 != 0)
leapYear = false;

return leapYear;
}

int calcNumDays(int month, int year)
{
int numDays;

if (month == 1)
numDays = 31;
else if (month == 2)
{
if (isLeapYear(year))
numDays = 29;
else
numDays = 28;
}
else if (month == 3)
numDays = 31;
else if (month == 4)
numDays = 30;
else if (month == 5)
numDays = 31;
else if (month == 6)
numDays = 30;
else if (month == 7)
numDays = 31;
else if (month == 8)
numDays = 31;
else if (month == 9)
numDays = 30;
else if (month == 10)
numDays = 31;
else if (month == 11)
numDays = 30;
else if (month == 12)
numDays = 31;

return numDays;
}

int getOffset(int month, int year)
{
int sum = 0;

for (int yearCount = 1753; yearCount < year; yearCount++)
{
if (isLeapYear(yearCount))
sum += 366;
else
sum += 365;
}
for (int monthCount = 1; monthCount < month; monthCount++)
{
sum += calcNumDays(monthCount, month);
}

int offset = sum % 7;

return offset;
}

void displayHeader(int month, int year)
{
switch (month)
{
case 1:
cout << endl << "January, " << year << endl;
break;
case 2:
cout << endl << "February, " << year << endl;
break;
case 3:
cout << endl << "March, " << year << endl;
break;
case 4:
cout << endl << "April, " << year << endl;
break;
case 5:
cout << endl << "May, " << year << endl;
break;
case 6:
cout << endl << "June, " << year << endl;
break;
case 7:
cout << endl << "July, " << year << endl;
break;
case 8:
cout << endl << "August, " << year << endl;
break;
case 9:
cout << endl << "September, " << year << endl;
break;
case 10:
cout << endl << "October, " << year << endl;
break;
case 11:
cout << endl << "November, " << year << endl;
break;
case 12:
cout << endl << "December, " << year << endl;
break;
}
return;
}

void displayTable(int offset, int numDays)
{
cout << setw(4) << "Su"
<< setw(4) << "Mo"
<< setw(4) << "Tu"
<< setw(4) << "We"
<< setw(4) << "Th"
<< setw(4) << "Fr"
<< setw(4) << "Sa"
<< endl;

if (offset != 6)
{
for(int spaceCount = 0; spaceCount <= offset; spaceCount++)
{
cout << " ";
}
}
for(int dayCount = 1; dayCount <= numDays; dayCount++)
{
cout << setw(4) << dayCount;

if ((dayCount + offset + 1) % 7 == 0 && dayCount + 1 <= numDays)
cout << endl;
}
cout << endl;

return;
}




int main()
{
int month = getMonth();
int year = getYear();
int numDays = calcNumDays(month, year);
int offset = getOffset(month, year);

displayHeader(month, year);
displayTable(offset, numDays);
}

Here is the out put for December 1753 with what it actually needs to say included:

December, 1753
> Su Mo Tu We Th Fr Sa
> 1 2 3 4 5 6 7\n
Exp: 1\n
> 8 9 10 11 12 13 14\n
Exp: 2 3 4 5 6 7 8\n
> 15 16 17 18 19 20 21\n
Exp: 9 10 11 12 13 14 15\n
> 22 23 24 25 26 27 28\n
Exp: 16 17 18 19 20 21 22\n
> 29 30 31\n
Exp: 23 24 25 26 27 28 29\n
>
Exp: 30 31\n

Last edited on
> void displayTable(int offset, int numDays)
explain what's the offset, what does it represent, and which values may it take.
@urrutiaeric

Made a few minor changes to your code, but the days fall in their correct weekdays, now.

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Calendar Issues.cpp : main project file.

#include <iostream>
#include <iomanip>

using namespace std;


int numDays = 0;
int offset = 0 ;

int getMonth()
{
	int month = 0;
	do
	{
		cout << "Enter a month number: ";
		cin >> month;

		if (month < 1 || month > 12)
			cout << "Month must be between 1 and 12.\n";
	}
	while (month < 1 || month > 12);

	return month;
}

int getYear()
{
	int year = 0;
	do
	{
		cout << "Enter year: ";
		cin >> year;

		if (year < 1753)
			cout << "Year must be 1753 or later.\n";
	} while (year < 1753);

	return year;
}

bool isLeapYear(int year)
{
	bool leapYear = false;

	if (year % 4 == 0)
		leapYear = true;
	if (year % 100 == 0 && year % 400 != 0)
		leapYear = false;

	return leapYear;
}

int calcNumDays(int month, int year)
{
	int numDays;

	if (month == 1)
		numDays = 31;
	else if (month == 2)
	{
		if (isLeapYear(year))
			numDays = 29;
		else
			numDays = 28;
	}
	else if (month == 3)
		numDays = 31;
	else if (month == 4)
		numDays = 30;
	else if (month == 5)
		numDays = 31;
	else if (month == 6)
		numDays = 30;
	else if (month == 7)
		numDays = 31;
	else if (month == 8)
		numDays = 31;
	else if (month == 9)
		numDays = 30;
	else if (month == 10)
		numDays = 31;
	else if (month == 11)
		numDays = 30;
	else if (month == 12)
		numDays = 31;

	return numDays;
}

int getOffset(int month, int year)
{
	// Revised getOffset function
	int t[12] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
	int offset = year - ((month-1) < 2);
	offset = (offset + offset / 4 - offset / 100 + offset / 400 + t[month-1] + 1) % 7;
	// offset = 0 for Sunday, 1 for Monday.. etc.
	return offset;
}

void displayHeader(int month, int year)
{
	switch (month)
	{
	case 1:
		cout << endl << "January, " << year << endl;
		break;
	case 2:
		cout << endl << "February, " << year << endl;
		break;
	case 3:
		cout << endl << "March, " << year << endl;
		break;
	case 4:
		cout << endl << "April, " << year << endl;
		break;
	case 5:
		cout << endl << "May, " << year << endl;
		break;
	case 6:
		cout << endl << "June, " << year << endl;
		break;
	case 7:
		cout << endl << "July, " << year << endl;
		break;
	case 8:
		cout << endl << "August, " << year << endl;
		break;
	case 9:
		cout << endl << "September, " << year << endl;
		break;
	case 10:
		cout << endl << "October, " << year << endl;
		break;
	case 11:
		cout << endl << "November, " << year << endl;
		break;
	case 12:
		cout << endl << "December, " << year << endl;
		break;
	}
	return;
}

void displayTable(int offset, int numDays)
{
	cout << setw(4) << "Su"
		<< setw(4) << "Mo"
		<< setw(4) << "Tu"
		<< setw(4) << "We"
		<< setw(4) << "Th"
		<< setw(4) << "Fr"
		<< setw(4) << "Sa"
		<< endl;

	cout << setw(4) << "";// Setting starting number below Sunday
	for(int spaceCount = 1; spaceCount < offset; spaceCount++)
	{
		cout << setw(4) << " ";// Move spacing over for each offset 
	}

	for(int dayCount = 1; dayCount <= numDays; dayCount++)
	{
		cout << setw(4) << dayCount;

		if ((dayCount + offset) % 7 == 0) // Don't need the '+1' or if dayCount > numDays,
			//since loop CAN'T go beyond numDays 
				cout << endl;
	}
	cout << endl;

	return;
}

int main()
{
	int month = getMonth();
	int year = getYear();
	int numDays = calcNumDays(month, year);
	int offset = getOffset(month, year);

	displayHeader(month, year);
	displayTable(offset, numDays);
}
Topic archived. No new replies allowed.