Help with calendar project

Hello. I'm a beginner, so I'm sorry if the the problem is ridiculous or easy to solve. I have to create a project for my CS124 class where I prompt the user for a month (1 - 12), and a year (1753+). With this information, the program displays the calendar for that chosen month/year.

I've been able to write the entire code, but something is off. I feel there's something wrong with the computeOffset(), isLeapYear(), or numDaysInMonth().

Any help is really appreciated.

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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <iostream>
#include <iomanip>
using namespace std;

int getMonth();
int getYear();
int computeOffset(int month, int year);
int numDaysInMonth(int month, int year);
int numDaysInYear(int year);
bool isLeapYear (int year);
void displayHeader(int month, int year);
void displayTable(int offset, int daysMonth);
void display(int month, int year, int offset, int daysMonth);

/**********************************************************************
* MAIN
* This program will prompt the user for the numeric month and year
* and will display a calendar table of the chosen month and year.
**********************************************************************/
int main()
{
   int month = getMonth();
   int year = getYear();
   int offset = computeOffset(month, year);
   int daysMonth = numDaysInMonth(month, year);
   int daysYear = numDaysInYear(year);
   isLeapYear(year);
   display(month, year, offset, daysMonth);

   return 0;
}

/*********************************************************************
* GET MONTH
* This function will prompt the user for the numeric month.
*********************************************************************/
int getMonth()
{
   int month;
   cout << "Enter a month number: ";
   cin  >> month;

   while (month < 1 || month > 12)
   {
      cout << "Month must be between 1 and 12.\n";
      cout << "Enter a month number: ";
      cin  >> month;
   }

   return month;
}

/*********************************************************************
* GET YEAR
* This function will prompt the user for the numeric year.
*********************************************************************/
int getYear()
{
   int year;

   cout << "Enter year: ";
   cin  >> year;

   while (year < 1753)
   {
      cout << "Year must be 1753 or later.\n";
      cout << "Enter year: ";
      cin  >> year;
   }

   return year;
}

/*********************************************************************
* IS LEAP YEAR
* This function will check if the year is a leap year or not.
*********************************************************************/
bool isLeapYear(int year)
{
   if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
      return true;
   else
      return false;
}

/********************************************************************
* NUM DAYS IN MONTH
* This function will take the month and year parameters given
* by the user and return the number of days in the chosen month.
*********************************************************************/
int numDaysInMonth(int month, int year)
{
   int daysMonth;

   if (month == 1 || month == 3 || month == 5 || month == 7 ||
       month == 8 || month == 10 || month == 12)
   {
      daysMonth = 31;
   }

   if (month == 4 || month == 6 || month == 9 || month == 11)
   {
      daysMonth = 30;
   }

   if (month == 2)
   {
      if (isLeapYear(year))
         daysMonth = 29;
      else
         daysMonth = 28;
   }
   return daysMonth;
}

/*********************************************************************
 * NUM DAYS IN YEAR
 * This function will take the year parameter given by the user
 * and return the number of days in the year, afer checking
 * if the year is a leap year.
 *********************************************************************/
int numDaysInYear(int year)
{
   int daysYear;

   if (isLeapYear(year))
      daysYear = 366;
   else
      daysYear = 365;

   return daysYear;
}

/*********************************************************************
* COMPUTE OFFSET
* This function will compute the offset, where the first day
* of the month will be. It will use two parameters: numDaysInMonth
* and numDaysInYear.
*********************************************************************/
int computeOffset(int month, int year)
{
   int numDaysAllYears = 0;
   int numDaysRemainingMonths = 0;

   for (int yearCount = 1753; yearCount < year; yearCount++)
   {
      numDaysAllYears += numDaysInYear(year);
   }

   for (int monthCount = 1; monthCount < month; monthCount++)
   {
      numDaysRemainingMonths += numDaysInMonth(month, year);
   }

   int offset = (numDaysAllYears + numDaysRemainingMonths) % 7;

   return offset;
}

/********************************************************************
* DISPLAY HEADER
* This function will display the header of the calendar.
*********************************************************************/
void displayHeader(int month, int year)
{
   cout << endl;

   if (month == 1)
      cout << "January";
   else if (month == 2)
      cout << "February";

   else if (month == 3)
      cout << "March";

   else if (month == 4)
      cout << "April";

   else if (month == 5)
      cout << "May";

   else if (month == 6)
      cout << "June";

   else if (month == 7)
      cout << "July";

   else if (month == 8)
      cout << "August";

   else if (month == 9)
      cout << "September";

   else if (month == 10)
      cout << "October";

   else if (month == 11)
      cout << "November";

   else if (month == 12)
      cout << "December";

   cout << ", " << year << "\n";

   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";
}

/*****************************************************************
* DISPLAY TABLE
* This function will display the calendar table.
******************************************************************/
void displayTable(int offset, int daysMonth)
{
   int dayWeek;
   int day;

   if (offset == 0)
   {
      day = 2;
      cout << setw(6);
   }

   else if (offset == 1)
   {
      day = 3;
      cout << setw(10);
   }

   else if (offset == 2)
   {
      day = 4;
      cout << setw(14);
   }

   else if (offset == 3)
   {
      day = 5;
      cout << setw(18);
   }

   else if (offset == 4)
   {
      day = 6;
      cout << setw(22);
   }

   else if (offset == 5)
   {
      day = 7;
      cout << setw(26);
   }

   else if (offset == 6)
   {
      day = 1;
      cout << setw(2);
   }

   for (dayWeek = 1; dayWeek <= daysMonth; dayWeek++)
   {
      cout << "  " << setw(2) << dayWeek;
      ++day;

      if (day == 8)
      {
         cout << "\n";
         day = 1;
      }
   }

   if (day >= 2 && day <= 7)
      cout << "\n";
}

/*********************************************************************
 * DISPLAY
 * This function will display the information. It contains the
 * header from displayHeader() and the calendar table from
 * displayTable().
 *********************************************************************/
void display(int month, int year, int offset, int daysMonth)
{
   displayHeader(month, year);
   displayTable(offset, daysMonth);
}
Last edited on
but something is off.

What does this mean?

I feel there's something wrong with the computeOffset()


Which is correct the comments or the actual parameters?
1
2
3
4
5
6
7
/*********************************************************************
* COMPUTE OFFSET
* This function will compute the offset, where the first day
* of the month will be. It will use two parameters: numDaysInMonth
* and numDaysInYear.
*********************************************************************/
int computeOffset(int month, int year)


I feel there's something wrong with the computeOffset(), isLeapYear(), or numDaysInMonth().

So what have you done to check your assumptions?

Have you individually tested each of these functions?

For example have you passed a known year that is either a leap year or not into isLeapYear() ?

Does it return the proper value for the given year?

The numDaysInMonth() should be fairly easy to check by passing in each of the months and then printing the number of days.

I came to this conclusion:
((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0))

But don't trust me when it comes to calculations xD
Last edited on
@jlb

Thanks for the reply. The actual parameters are correct. I forgot to change it in the comments. Thanks for the heads up.

I've been working on it all day and I'm having a hard time figuring out where the error is.

I've checked many times, many different combinations of months and years, and I get mixed results. For example, when I use January 1753 (the first possible month), it uses the correct offset of 0 (monday) and creates the table correctly.

However, when I use February of 1753 (the next month), it creates the table using offset 0 as well.

Another example is when I use February 1800 (which is a leap year), it correctly uses the offset 6 (Saturday), and returns 29 days.

So that's why it's so confusing to me. For some dates it's returning the correct offset, and for others it's not.
@Marcus Aseth

I tried making that change and nothing changed. February 1753 is still being created with offset 0 (starting on Monday).
I've checked many times, many different combinations of months and years, and I get mixed results. For example, when I use January 1753 (the first possible month), it uses the correct offset of 0 (monday) and creates the table correctly.

You need to narrow down the problem to find out where the problem actually is. Just guessing is not going to solve the problem. So start by testing each of the functions separately.

Something like the following (it could be simpler but this should give you the idea):

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
#include <iostream>

bool isLeapYear(int year)
{
    if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        return true;
    else
        return false;
}


int main()
{
    int test_year = -1;

    std::cout << "Please enter a known leap year (0 to exit): ";
    std::cin >> test_year;
    while(test_year != 0)
    {

        if(isLeapYear(test_year))
        {
            std::cout << "\nThe test year is a leap year!\n";
        }
        else
        {
            std::cout << "\nThe function computes that the test year is not a leap year\n\n";
            std::cout << "Are you sure the year you entered is indeed a leap year?\n\n";
        }
        std::cout << "\nPlease enter a known leap year (0 to exit): ";
        std::cin >> test_year;
    }

    test_year = -1;
    std::cout << "Please enter a known normal year (0 to exit): ";
    std::cin >> test_year;

    while(test_year != 0)
    {
        if(!isLeapYear(test_year))
        {
            std::cout << "\nThe test year is not a leap year!\n";
        }
        else
        {
            std::cout << "\nThe function computes that the test year is a leap year\n\n";
            std::cout << "Are you sure the year you entered is indeed not a leap year?\n\n";
        }
        std::cout << "\nPlease enter a known normal year (0 to exit): ";
        std::cin >> test_year;
    }

    return(0);
}


If this program gives the proper results for known years then you move to the next function and write a "test" for that function.




Last edited on
Topic archived. No new replies allowed.