calendar program offset

Hello, I am writing a computer program to display the calendar of a given month and year. It seems like my loop to display the new week isn't working. I have used division by 7 to do so. All of my other functions work great, so this displayTable function I feel like is the problem. The code is beneath. I only posted relevant functions, but if you need more code to help solve the issue I would be happy to post more. I also took the 1 away that adds the offset and still it just the whole month on one line. I don't understand why it doesn't work, because I have used it before in a different program and that one gave me no issues.
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
  /**********************************************************************
 * This function will determine the offset which determines what the
 * first day of the month starts on.
 ***********************************************************************/
int offSetDay(int yearNumber, int monthNumber)
{
int offSet;

offSet = (totalDaysPastYears(yearNumber) + totalDaysinCurrentYear
          (yearNumber, monthNumber)) % 7;

return offSet;
}
/**********************************************************************
 * This function will tell us how many days are in a given
 * month.
 ***********************************************************************/

int numDaysinMonth(int monthNumber)
{
   int yearNumber;

   if (monthNumber == 1)
      return 31;

   else if (monthNumber == 2)
   {
      if (isLeapYear(yearNumber) == true)
     return 29;
   else
      return 28;
   }
   else if (monthNumber == 3)
      return 31;

   else if (monthNumber == 4)
      return 30;

   else if (monthNumber == 5)
      return 31;
else if (monthNumber == 6)
      return 30;

   else if (monthNumber == 7)
      return 31;

   else if (monthNumber == 8)
      return 31;

   else if (monthNumber == 9)
      return 30;

   else if (monthNumber == 10)
      return 31;

   else if (monthNumber == 11)
      return 30;

   else if (monthNumber == 12)
      return 31;

}
/**********************************************************************
 * This function will display a calendar that will start given the
 * and end depending on how many days are in the month.
 ***********************************************************************/
void displayTable(int yearNumber, int monthNumber)
{
   int days;

   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";

   if (offSetDay(yearNumber, monthNumber) == 0)
      cout << setw(5); //Monday

   else if (offSetDay(yearNumber, monthNumber) == 1) //Tuesday
      cout << setw(9);

   else if (offSetDay(yearNumber, monthNumber) == 2) //Wednesday
      cout << setw(13);

   else if (offSetDay(yearNumber, monthNumber) == 3) //Thursday
      cout << setw(17);

   else if (offSetDay(yearNumber, monthNumber) == 4) //Friday
      cout << setw(21);

   else if (offSetDay(yearNumber, monthNumber) == 5) //Saturday
      cout << setw(25);

   else if (offSetDay(yearNumber, monthNumber) == 6) //Sunday
      cout << setw(1);

   for (days = 1; days <= numDaysinMonth(monthNumber); days++)
  {
      cout  <<  " "  << setw(3) <<  days;
      if (days + 1 + offSetDay(yearNumber, monthNumber)
          % 7 == 0 &&  days < numDaysinMonth(monthNumber))
         cout << "\n";
   }
}
/*********************************************************************
 * Main will call the functions above to display a month calendar of
 * a given year.
 *********************************************************************/
int main()
{
   int monthNumber;
   int yearNumber;
   int offSet;

  monthNumber = getMonth();
  yearNumber = getYear();
  displayMonth(monthNumber);
  cout << yearNumber << endl;
  displayTable(yearNumber, monthNumber);
  cout << endl;

  return 0;
}



Last edited on
@alextexasfan12

This seems, to me anyway, the same problem you were helped out with, at
http://www.cplusplus.com/forum/beginner/216320/
. titled, 'space days apart'. I even showed you how to display the calendar correctly, starting the first day on the correct day of the week. How is this question asking for anything different than that one?
yeah I tried to use that code, but since it didn't work like I needed to I used a series of if statements to make it work. However, I did change the code to what you suggested in your past post, and it's still not making a new line after a new week, I could be missing something, but I'm pretty sure I got it copied correctly. I guess what I mean is that my code used to let me go to a new week, but for some reason it's not letting me go to a new week anymore, and has all the numbers of the month in all in one line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

void displayTable(int yearNumber, int monthNumber)
{
   int days;

   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";

   for (int x = 0; x < offSetDay(yearNumber, monthNumber); x++)
           cout << "  ";
   for (days = 1; days <= numDaysinMonth(monthNumber); days++)
   {
      cout  <<  " "  << setw(3) << days;
      if (days + offSetDay(yearNumber, monthNumber)
          % 7 == 0)
         cout << "\n";
   }


}
Last edited on
With numDaysinMonth it's easier to store the number of days per month in an array, then look them up.

Did you look at JLBorges code in your other topic?

Pleas don't start new topics about the same subject, it's ultimately a time waster for those who reply.
Topic archived. No new replies allowed.