Leap year Fix.

Hello all,
I am very new to C++
here is a calender code that prints each date of the month that the user types

example: cout " Please enter month and year"
it will print each day
Here is the Code below.

Somehow the Leap year isn't working. Can someone help me figure out what is missing or what is wrong with it?



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




#include "std_lib_facilities_3.h"
 // #include <iostream.h>

bool LeapYear (int year)

// "year" must be between 1900 and 2999.
// Returns true if "year" is a leap year.

{
// true, if year is divisible by 4, and ...
// ... either not divisible by 100, or divisible by 400. 

return (year % 4 == 0 &&
(year % 100 != 0 || year % 400 == 0));
}

//**************************************

const int daysInMonth[12] = {

31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};

// Days from the beginning of the year
// to the beginning of the month:

const int daysToMonth[12] = {
0, 31, 59, 90, 120, 151,
181, 212, 243, 273, 304, 334
};

const char *dayName[7] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};

const int DAY0 = 1; // Day of week for 01-01-1900 is Monday = 1.

//**************************************

bool ValidDate (int month, int day, int year)

// Returns true if month-day-year is a valid date between
// 01-01-1900 and 12-31-2999.

{
bool valid = false; // First assume the date is invalid.
int days;

// If year and month have valid values:
if (year >= 1900 && year <= 2999 &&
month >= 1 && month <= 12) {

// Get the number of days in this month from the table:
days = daysInMonth[month-1]; // (-1, because the indices
// of an array start from 0.)

// If February of a leap year -- increment the number
// of days in this month:
if (month == 2 && LeapYear(year))
days++;

// Check that the given day is within the range.
// If so, set valid to true:
if (day >= 1 && day <= days)
valid = true;
}

return valid;
}

//**************************************

long DaysSince1900 (int month, int day, int year)

// Returns the number of days elapsed since 01-01-1900
// to month-day-year

{
long days;

// Calculate days to 01-01 of this year with correction for
// all the previous leap years:
if (year == 1900)
days = 0;
else
days = long(year - 1900) * 365
+ (year - 1901) / 4 // +1 for each 4th year
- (year - 1901) / 100 // -1 for each 100th year
+ (year - 1601) / 400; // +1 for each 400th year,
// starting at 2000

// Add days for previous months with correction for
// the current leap year:
days += daysToMonth[month-1];
if (LeapYear(year) && month > 2) days++;

// Add days since the beginning of the month:
days += day - 1;

return days;
}

//**************************************

int DayOfWeek (int month, int day, int year)

// Returns the day of the week for a given date:
// 0 -- Sunday, 1 -- Monday, etc.

{
return int((DAY0 + DaysSince1900(month, day, year)) % 7);
}

int main()

{
int month, day = 1, year, days, j;
int weekday;

cout << "Please enter a month and a year==> ";
cin >> month >> year;

if (!ValidDate(month, day, year)) {
cout << "*** Invalid date ***\n";
return 1;
}

if(month == 1) days = 31;
if(month == 2) days = 28;
if(month == 3) days = 31;
if(month == 4) days = 30;
if(month == 5) days = 31;
if(month == 6) days = 30;
if(month == 7) days = 31;
if(month == 8) days = 31;
if(month == 9) days = 30;
if(month == 10) days = 31;
if(month == 11) days = 30;
if(month == 12) days = 31;

for(j = 1; j <= days; j++)
{
cout << j << " ";

weekday = DayOfWeek(month, j, year);
if(weekday == 0) cout << "Sunday";
if(weekday == 1) cout << "Monday";
if(weekday == 2) cout << "Tuesday";
if(weekday == 3) cout << "Wednesday";
if(weekday == 4) cout << "Thursday";
if(weekday == 5) cout << "Friday";
if(weekday == 6) cout << "Saturday";

cout << endl;
}

return 0;
}


I tested it on leap year days, and it still only prints upto 28.
What you do is what you get. Here in main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(month == 1) days = 31;
if(month == 2) days = 28;
if(month == 3) days = 31;
if(month == 4) days = 30;
if(month == 5) days = 31;
if(month == 6) days = 30;
if(month == 7) days = 31;
if(month == 8) days = 31;
if(month == 9) days = 30;
if(month == 10) days = 31;
if(month == 11) days = 30;
if(month == 12) days = 31;

for(j = 1; j <= days; j++)
{
cout << j << " ";

you always assign 28 to days if month equal to 2.

Local variable with the same name in function ValidDate

1
2
3
4
5
6
7
8
bool ValidDate (int month, int day, int year)

// Returns true if month-day-year is a valid date between
// 01-01-1900 and 12-31-2999.

{
bool valid = false; // First assume the date is invalid.
int days;

is not used in main.
Thank you sir, I fixed it :).

Topic archived. No new replies allowed.