What is wrong with my code?

I am to make a code for a calendar as a school project, and I almost have it finished. However, when the application is run, the number of days for the month continues until the year number. I believe it has to do with the part that gives out the days in the month(int numDaysMonth). I need to keep it as an array, since that is apart of the rubric.


This is the main:

#include <cstdlib>
#include <iostream>
#include "LeapYear.h"
#include "calendar.h"
using namespace std;

int main(int argc, char *argv[])
{
functions();
system("PAUSE");
return EXIT_SUCCESS;
}




This is where the calendar is created (a separate file linked to the others)...


#include <iostream>
#include <iomanip>
#include "LeapYear.h"
#include"calendar.h"
using namespace std;



/**********************************************************************
* This function will call all the functions necessary to make a calendar
* for any given month and year.
***********************************************************************/
int functions()
{
int numDays;
int offset;
int month;
int year;

month = getMonth(month);

year = getYear(year);

offset = computeOffset(year, month);

display(year, month, offset);

return 0;
}

/***********************************************************************
* Gets the month number.
**********************************************************************/
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;
}

/***********************************************************************
* Gets the year.
**********************************************************************/
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;
}

/***********************************************************************
* Computes the offset.
**********************************************************************/

int computeOffset(int year, int month)
{
int offset = 0;
int count = year - 1753;
for ( int iYear = 0; iYear < count; iYear++)
{
offset = ( offset + 365 + isLeapYear(year)) % 7;
}

for ( int iMonth = 1; iMonth < month; iMonth++)
{
offset = ( offset + numDaysMonth(year, iMonth)) % 7;
}

return offset;
}



/***********************************************************************
* Computes the number of days in the given year.
**********************************************************************/
int numDaysYear(int year)
{
int daysYear = 365 + isLeapYear(year);
return daysYear;
}

/***********************************************************************
* Gets the number of days in the given month.
**********************************************************************/
int numDaysMonth(int year, int month)
{
int daysMonth[13];
daysMonth[0] = 31;
daysMonth[1] = 29;
daysMonth[2] = 28;
daysMonth[3] = 31;
daysMonth[4] = 30;
daysMonth[5] = 31;
daysMonth[6] = 30;
daysMonth[7] = 31;
daysMonth[8] = 31;
daysMonth[9] = 30;
daysMonth[10] = 31;
daysMonth[12] = 30;
daysMonth[13] = 31;
if ( month == 1)
daysMonth[0];
else if ( month == 2)
{
if (isLeapYear(year) == true)
daysMonth[1];
else
daysMonth[2];
}
else if ( month == 3)
daysMonth[3];
else if ( month == 4)
daysMonth[4];
else if ( month == 5)
daysMonth[5];
else if ( month == 6)
daysMonth[6];
else if ( month == 7)
daysMonth[7];
else if ( month == 8)
daysMonth[8];
else if ( month == 9)
daysMonth[9];
else if ( month == 10)
daysMonth[10];
else if ( month == 11)
daysMonth[11];
else if ( month == 12)
daysMonth[12];

}


/**********************************************************************
* Displays the calender table.
**********************************************************************/
void display(int year, int month, int offset)
{
int dayOfWeek;
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 << "\n";
// Display month header
cout << " Su Mo Tu We Th Fr Sa\n";

// Gets the correct offset width and end the line on the right
//day of the week
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";

// The loop for displaying the days and ending the line in the right place
for ( dayOfWeek = 1; dayOfWeek <= numDaysMonth(year, month); dayOfWeek++ )
{
cout << " " << setw(2) << dayOfWeek;
++day;
if (day == 8)
{
cout << "\n";
day = 1;
}
}
if ( day >= 2 && day <= 7)
cout << "\n";

}





Then I have a separate file linked to that one:

#include <iostream>
#include <iomanip>
#include "LeapYear.h"
#include"calendar.h"

using namespace std;

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



Then the first header file...

bool isLeapYear(int year);



And the second header file...

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 display(int year, int month, int offset);


Last edited on
I don't have a lot of time to work through your code. A help that you could give us is to put code tags around the code that you paste into your post. Click the format button that looks like "<>", and paste your code between the tags that it generates. You can even edit your original post, highlight the lines of a file and click the button, and the code formatting should take effect.

On to your code. Based on a very cursory review of you code, I found that you messed up populating your daysMonth array. You never populated daysMonth[11], and populated daysMonth[13] instead. Remember, when you declare an array to be 'n' elements (13 in your case), the valid indices are 0 to n-1 (0 - 12).
I fixed that, however I'm still having the same problem..

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
#include <iostream>
#include <iomanip>
#include "LeapYear.h"
#include"calendar.h"
using namespace std;



/**********************************************************************
 * This function will call all the functions necessary to make a calendar
 * for any given month and year.
 ***********************************************************************/
int functions()
{
   int numDays;
   int offset;
   int month;
   int year;

   month = getMonth(month);

   year = getYear(year);

   offset = computeOffset(year, month);

   display(year, month, offset);

   return 0;
}

/***********************************************************************
 * Gets the month number.
 **********************************************************************/
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;
}

/***********************************************************************
 * Gets the year.
 **********************************************************************/
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;
}

/***********************************************************************
 * Computes the offset.
 **********************************************************************/

int computeOffset(int year, int month)
{
   int offset = 0;
   int count = year - 1753;
   for ( int iYear = 0; iYear < count; iYear++)
   {
      offset = ( offset + 365 + isLeapYear(year)) % 7;
   }

   for ( int iMonth = 1; iMonth < month; iMonth++)
   {
      offset = ( offset + numDaysMonth(year, iMonth)) % 7;
   }

   return offset;
}



/***********************************************************************
 * Computes the number of days in the given year.
 **********************************************************************/
int numDaysYear(int year)
{
   int daysYear = 365 + isLeapYear(year);
   return daysYear;
}

/***********************************************************************
 * Gets the number of days in the given month.
 **********************************************************************/
int numDaysMonth(int year, int month)
{
   int daysMonth[13];
   daysMonth[0] = 31;
   daysMonth[1] = 29;
   daysMonth[2] = 28;
   daysMonth[3] = 31;
   daysMonth[4] = 30;
   daysMonth[5] = 31;
   daysMonth[6] = 30;
   daysMonth[7] = 31;
   daysMonth[8] = 31;
   daysMonth[9] = 30;
   daysMonth[10] = 31;
   daysMonth[11] = 30;
   daysMonth[12] = 31;
   if ( month == 1)
      daysMonth[0];
   else if ( month == 2)
   {
      if (isLeapYear(year) == true)
         daysMonth[1];
      else
         daysMonth[2];
   }
   else if ( month == 3)
      daysMonth[3];
   else if ( month == 4)
      daysMonth[4];
   else if ( month == 5)
      daysMonth[5];
   else if ( month == 6)
      daysMonth[6];
   else if ( month == 7)
      daysMonth[7];
   else if ( month == 8)
      daysMonth[8];
   else if ( month == 9)
      daysMonth[9];
   else if ( month == 10)
      daysMonth[10];
   else if ( month == 11)
      daysMonth[11];
   else if ( month == 12)
      daysMonth[12];

}


 /**********************************************************************
 * Displays the calender table.
 **********************************************************************/
void display(int year, int month, int offset)
 {
    int dayOfWeek;
    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 << "\n";
    // Display month header
    cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";

    // Gets the correct offset width and end the line on the right
    //day of the week
    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";

    // The loop for displaying the days and ending the line in the right place
    for ( dayOfWeek = 1; dayOfWeek <= numDaysMonth(year, month); dayOfWeek++ )
    {
       cout << "  " <<  setw(2) << dayOfWeek;
       ++day;
       if (day == 8)
       {
          cout << "\n";
          day = 1;
       }
    }
    if ( day >= 2 && day <= 7)
       cout << "\n";

 }

Topic archived. No new replies allowed.