Hello, I'm working on a Calendar program that will display the calendar for any month during any year past the year 1758. The program will need to calculate several things in order to correctly display this information, but one important detail is that it has to calculate the offset of the 1st of the selected month within that year. For example, on January 1, 1758, it is a Monday, meaning the offset is 0. If it were a Thursday, the offset would be 3, seeing as it's 3 days away from Monday, which is considered the "start" of the offset.
In order to calculate this, I have to add up all of the days from 0 to the input year the user gives, and then mod that by 7, which will give me the correct offset. However, after creating my code, it will only produce an offset of 2. I can't seem to find my error.
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
|
int startYear = 1758;
int month;
int year;
int offset = 0;
int numDays = 0;
/*********************************************************************
* This function will receive the year input from the user.
*********************************************************************/
int getYear()
{
cout << "Enter year: ";
cin >> year;
if (year < startYear)
{
cout << "Year must be 1753 or later." << endl;
cout << "Please enter a year: ";
cin >> year;
}
return year;
}
/*********************************************************************
* This function will receive the month input from the user.
*********************************************************************/
int getMonth()
{
cout << "Enter a month number: ";
cin >> month;
if (month > 12 || month < 1)
{
cout << "Month must be between 1 and 12." << endl;
cout << "Enter a month number: ";
cin >> month;
}
return month;
}
/**********************************************************************
* This function will determine if it is a leap year or not.
***********************************************************************/
bool isLeapYear(int year) {
if (year % 4 == 0)
{
if (year % 100 == 0 && year % 400 != 0)
return false;
else
return true;
}
else
return false;
}
/*********************************************************************
* This function will calculate the offset of the first day of the month
*********************************************************************/
int computeOffset(int month, int year)
{
for (numDays = 0; year > 1; year--)
{
numDays += 365;
if (isLeapYear(year))
numDays += 1;
}
offset = numDays % 7;
return offset;
}
/*********************************************************************
* Main will run all of the functions to display the calendar
*********************************************************************/
int main()
{
getMonth();
getYear();
cout << "Offset: " << computeOffset(month, year) << endl;
return 0;
}
|
From what I can tell, the problem is in my for loop, but I'm not entirely certain. Maybe there's some minor detail I'm missing. In any case, any help I could get would be very appreciated. Thank you very much!