Calendar program

Hello everyone I have a question regarding my code. The program is supposed to show 0-7 for the offset. This starts on a Sunday until Saturday. It is supposed to count the days and then use the % 7 to display those.


THIS IS MY CODE
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;
}
bool isLeapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return 366;
else 365;
}

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++)
{
numDays += isLeapYear(year);

}

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;
}
You didn't really ask a question, but here is what I see wrong:

Line 32: You're returning number of days per year in a bool function. Both values are true.

line 33: 366 is not a statement (it is, but a not a useful one). You're missing the return

line 73: numdays is an uninitialized variable.

line 76,80: Incrementing an uninitialized variable.

Line 76: Trying to add a bool to an integer.

Line 99-100: You're calling getMonth() and getYear(), but ignoring the result.

Line 101: You're calling computeOffset with uninitialized variables.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Last edited on
Topic archived. No new replies allowed.