Calendar Program Offset

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!
Last edited on
@jaykeblakk

This function jumps out to me as your main problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int computeOffset(int month, int year)
{
  for (numDays = 0;  year > 1; year--)// It seems it should be more like
                                // for (int x=year; x>1753;x--) 
                                // x = year entered. Subtract 1 year if greater than target year 1753
                               // Keep looping if x > 1753
                               // Using 1753 in accordance with your line 15
                              // If that was wrong, use your 1758 with x
       {
         numDays += 365;
         if (isLeapYear(year))
            numDays += 1;
      }
      offset = numDays % 7;
      return offset;
}


Also, by my program, January 1st, 1758, was a Sunday
@jaykeblakk

The isLeapYear function could be more concisely written like this:

1
2
3
4
5
6
7
bool isLeapYear(int year) {
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        return true;
    } else {
        return false;
    }
}



@whitenite1

Can you explain to me why my way isn't working, and this one will? I'm a little confused by the syntax.

Also, you're right. It should be 1753, not 1758. My mistake. Thank you for pointing that out.

@TheIdeasMan

That's a really clever way of putting it. I never thought of it that way. Thank you very much for that!
@jaykeblakk

Even better :+)

1
2
3
bool isLeapYear(int year) {
    return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? true : false;
}
A minor point, the ? true : false is redundant. It merely says if the result of evaluating the expression is true, return true, else return false.

More concisely, the same code:
1
2
3
bool isLeapYear(int year) {
    return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
Damn, I swear I have CPAD (Copy Paste Adaptation Disease) sometimes :+|
@jaykeblakk
Can you explain to me why my way isn't working, and this one will? I'm a little confused by the syntax.


Sure, no problem.

First, you're starting the loop with the variable you're trying to increase. numDays. So, even though you increase it by 365 in the loop, the next time through that loop, it becomes a zero again.

1
2
3
4
5
6
7
8
9
10
11
12
13
int computeOffset(int month, int year)
{
  for (numDays = 0;  year > 1; year--)// It seems it should be more like
                                // for (int x=year; x>1753;x--)          
       {
         numDays += 365;
         if (isLeapYear(year)) // And this should be -- if (isLeapYear(x)), since you're
                                  // checking if a certain year designated as x, IS a leap year
            numDays += 1;
      }
      offset = numDays % 7;
      return offset;
}


None of your program results are of any use, though. You're not get getting the day for the month and year, entered, plus you're not getting the current month, day and year, to start counting back from. When I run your program, I always get a 6 for offset. That's not always going to be the correct answer.
Last edited on
Topic archived. No new replies allowed.