Help with offset function for a calendar program

Need help with calendar program!
The problem I think is in the offset function. This calendar is supposed to display a calendar starting on January of 1753. If we put January 1753 it should start on a Monday and so on. So the offset function needs to calculate that and it should display the numbers accordingly.


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
#include <iostream>
#include <iomanip>
using namespace std;

int getMonth(int month);
int getYear(int year);
int computeOffset(int year, int month);
int numDaysYear(int year);
int numDaysMonth(int year, int month);
bool isLeapYear(int year);
void displayDate(int year, int month, int offset);

/**********************************************************************
 * The function Main is meant to be in every program. While it is not
 * necessary it tells your computer where to start. It is called
 * a program start up.
 ***********************************************************************/
int main()
{
   int offset;
   int month;
   int year;
   month = getMonth(month);
   year = getYear(year);
   offset = computeOffset(year, month);
   displayDate(year, month, offset);
   return 0;
}

/***********************************************************************
 * Asks for a month, numbers between 1 and 12.
 **********************************************************************/
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"
           << "Enter a month number: ";
      cin >> month;
   }

   return month;
}

/***********************************************************************
 * Asks for a year greater than 1753.
 **********************************************************************/
int getYear(int year)
{
   cout << "Enter year: ";
   cin >> year;

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

/***********************************************************************
 * This is where my problem started, I couldn't quite get what it
 * wanted from this function, tried to write it as closer as to what
 * you showed us, but couldn't get it to work, everyone said I should
 * check the leap year, but I saw no problem with it.
 **********************************************************************/
int computeOffset(int month, int year)
{
   int numDays = 0;
   for (int yearCount = 1753; yearCount < year; yearCount++)
   {
      if (isLeapYear(year) == true)
         numDays += 366;
      else
         numDays += 365;

   }

   for (int monthCount = 1; monthCount < month; monthCount++)
 {
      numDays += numDaysMonth(year,month);
   }

   return numDays % 7;
}

/***********************************************************************
 * Will calculate the number of months corrseponding to the year you
 * entered.
 **********************************************************************/
int numDaysMonth(int year, int month)
{
   int daysMonth;

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

   return daysMonth;
}

/***********************************************************************
 * Determines if the year you input is a leap year.
 **********************************************************************/
bool isLeapYear(int year)
{
   if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
      return true;
}


/**********************************************************************
 * This function will display the date and the calendar table.
 **********************************************************************/
void displayDate(int year, int month, int offset)
{
   int dayWeek;
   int day;

   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 << endl;
//Displays the table
   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";

   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);
   }
   else
      cout << "Error offset must be >= 0 and <=6\n";

  for ( dayWeek = 1; dayWeek <= numDaysMonth(year, month); dayWeek++ )
   {
      cout << "  " <<  setw(2) << dayWeek;
      ++day;
      if (day == 8)
      {
         cout << "\n";
         day = 1;
      }
   }
   if ( day >= 2 && day <= 7)
      cout << "\n";

   return;
}


I think it will be easier to understand if you just run this code. As I mentioned the offset when putting month: 1 and year 1753 offset should be 0.

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
#include <iostream>
#include <iomanip>
using namespace std;
int getMonth();
int getYear();


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

int getYear()

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

   while ( year < 1753)
   {
      cout << "Year must be 1753 or later.\n"
           << "Enter year: ";
      cin >> year;
   }
}
bool isLeapYear(int year)
{
   if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
      return true;
}


int numDaysMonth(int year, int month)
{
   int daysMonth;

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

   return daysMonth;
}

int computeOffset(int month, int year)
{
   int numDays;
   for (int yearCount = 1753; yearCount < year; yearCount++)
   {
      if (isLeapYear(year) == true)
         numDays += 366;
      else
         numDays += 365;

   }

   for (int monthCount = 1; monthCount < month; monthCount++)
   {
      numDays += numDaysMonth(year,month);
   }

   return numDays % 7;

int main()
{
   int month;
   int year;
   getMonth();
   getYear();
   cout << "Offset: " << computeOffset(month,year)
        << endl;
   return 0;
}
Last edited on
You should be getting some warnings from your compiler that need to be fixed. If you're not receiving any warnings then you need to see about increasing your compiler warning level. Here are the warnings given by the online compiler:

In function 'bool isLeapYear(int)': 139:1: warning: control reaches end of non-void function [-Wreturn-type] In function 'int main()': 23:27: warning: 'month' is used uninitialized in this function [-Wuninitialized] 24:24: warning: 'year' is used uninitialized in this function [-Wuninitialized] In function 'int numDaysMonth(int, int)': 129:11: warning: 'daysMonth' may be used uninitialized in this function [-Wmaybe-uninitialized] In function 'void displayDate(int, int, int)': 223:12: warning: 'day' may be used uninitialized in this function [-Wmaybe-uninitialized]


By the way these warnings are for the first program, since the second program has several typos that cause the program to fail to compile.

Last edited on
Thanks for the response jlb, but my compiler runs the first code, we are using a tool caled PuTTy, so I don't know how high my compiler warning level is. It seems like it is not too high since it doesn't tells you any of that.
But let me re-post the second code, I might have missed some 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
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
#include <iostream>
#include <iomanip>
using namespace std;
int getMonth();
int getYear();



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

int getYear()

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

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

void displayDate(int year, int month)
{
   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";
   return;
}

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

int numDaysMonth(int year, int month)
{
   int daysMonth;

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

   return daysMonth;
}

int computeOffset(int month, int year)
{
   int numDays;
   for (int yearCount = 1753; yearCount < year; yearCount++)
   {
      if (isLeapYear(year) == true)
         numDays += 366;
      else
         numDays += 365;

   }

   for (int monthCount = 1; monthCount < month; monthCount++)
   {
      numDays += numDaysMonth(year,month);
   }

   return numDays % 7;
}

int main()
{
   int month;
   int year;
   getMonth();
   getYear();
   cout << "Offset: " << computeOffset(month,year)
        << endl;
   return 0;
}


I tried compiling it and it works. So I think it should compile now.
So my question here or this is what is expected to do.

These are the instructions.
Write the code necessary to determine what day of the week on which a given month starts or begins. In other words, to compute the offset. In this case, the offset begins on Monday.

Day------------offset
Sunday----------6
Monday----------0
Tuesday---------1
Wednesday-----2
Thursday --------3
Friday-------------4
Saturday---------5


Example

Two examples. The first is from January 1st which we know is a Monday:

Enter a month number: 1
Enter year: 1753
Offset: 0

The second is the 1st of August, 2001 which is a Wednesday.

Enter a month number: 8
Enter year: 2001
Offset: 2

You should be getting compile warnings that you need to fix!

From the online compiler (gear icon located outside the upper right side of the code block).

1
2
3
4
5
In function 'bool isLeapYear(int)': 75:1: warning: control reaches end of non-void function [-Wreturn-type] 
In function 'int main()': 140:50: warning: 'month' is used uninitialized in this function [-Wuninitialized] 
140:50: warning: 'year' is used uninitialized in this function [-Wuninitialized] 
In function 'int numDaysMonth(int, int)': 111:11: warning: 'daysMonth' may be used uninitialized in this function [-Wmaybe-uninitialized] 
In function 'int computeOffset(int, int)': 116:8: warning: 'numDays' may be used uninitialized in this function [-Wmaybe-uninitialized] 


Last edited on
Topic archived. No new replies allowed.