How to format a Calendar table

Hello all, I am very new to C++ and I am trying to format a Gregorian Calendar that accepts only the year as a user input, but I want the display to show 3 months on one row.

I think my loop inside of my calendar1 function specifically:
1
2
3
4
5
6
7
 
 //Is day before Sat? Else start next line Sun.
 if ( ( day + daycode ) % 7 > 0 )
  cout << right << setw(3);
 else
  cout << "\n ";
 

is making the print dates not print the way I'd like, but I can't seem to fix it.
*Hopefully the format is OK. I use codeblocks to run it.

Anything you can help with would be great. Thank you!
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

#include<iostream>
#include <string>
#include <iomanip>

using namespace std;

const int TRUE =  1;
const int FALSE =  0;

int days_in_month[7]={31,28,31,30,31,30};         
string months[7]=
{
 "JANUARY",
 "FEBRUARY",
 "MARCH",
 "APRIL",
 "MAY",
 "JUNE",
};


int inputyear()
{
 int year;
 cout << "What year do you want a calendar for? ";
 cin >> year;
 //Input validation for years between 2000 and 2020
 if ((year < 2000) || (year > 2020))
 {
  cout << year << " is invalid. Please enter a year between 2000 and 2020.";
  cout << "What year do you want a calendar for? " << endl;
  cin >> year;
 }
 return year;
}

int determinedaycode(int year)
{
 int daycode;
 int d1, d2, d3;
 
 d1 = (year - 1.)/ 4.0;
 d2 = (year - 1.)/ 100.;
 d3 = (year - 1.)/ 400.;
 daycode = (year + d1 - d2 + d3) %7;
 return daycode;
}


int determineleapyear(int year)
{
 if(year% 4 == FALSE && year%100 != FALSE || year%400 == FALSE)
 {
  days_in_month[2] = 29;
  return TRUE;
 }
 else
 {
  days_in_month[2] = 28;
  return FALSE;
 }
}

void calendar1(int year, int daycode)
{
 int month, day;

 cout << endl;
 cout << setw(40) << "Calendar year for " << year << endl;

 cout << "\n" << setw(15) << months[0];
 cout << setw(25) << months[1];
 cout << setw(20) << months[2];
 cout << endl;

    //Jan
 cout << setw(2) << "S";
 cout << setw(3) << "M" << setw(3) << "T";
 cout << setw(3) << "W" << setw(3) << "T";
 cout << setw(3) << "F" << setw(3) << "S";
 for ( month = 0; month < 1; month++ )
 {
 //Correct the position for the first date
  for ( day = 1; day <= 1 + daycode * 5; day++ )
  {
   cout << setw(2);
  }
  // Print all the dates for one month
  for ( day = 1; day <= days_in_month[month]; day++ )
  {
  cout << right << setw(2) << day;

  //Is day before Sat? Else start next line Sun.
  if ( ( day + daycode ) % 7 > 0 )
   cout << right << setw(3);
  else
  cout << "\n ";
  }
  // Set position for next month
  daycode = ( daycode + days_in_month[month] ) % 7;
 }
 //Feb
 cout << setw(5) << "S";
 cout << setw(3) << "M" << setw(3) << "T";
 cout << setw(3) << "W" << setw(3) << "T";
 cout << setw(3) << "F" << setw(3) << "S";
 for ( month = 1; month < 2; month++ )
 {
 //Correct the position for the first date
  for ( day = 1; day <= 1 + daycode * 5; day++ )
  {
   cout << setw(2);
  }
  // Print all the dates for one month
  for ( day = 1; day <= days_in_month[month]; day++ )
  {
  cout << right << setw(2) << day;

  //Is day before Sat? Else start next line Sun.
  if ( ( day + daycode ) % 7 > 0 )
   cout << right << setw(3);
  else
  cout << "\n ";
  }
  // Set position for next month
  daycode = ( daycode + days_in_month[month] ) % 7;
 }
 //Mar
 cout << setw(5) << "S";
 cout << setw(3) << "M" << setw(3) << "T";
 cout << setw(3) << "W" << setw(3) << "T";
 cout << setw(3) << "F" << setw(3) << "S";
 for ( month = 2; month < 2; month++ )
 {
 //Correct the position for the first date
  for ( day = 1; day <= 1 + daycode * 5; day++ )
  {
   cout << setw(2);
  }
  // Print all the dates for one month
  for ( day = 1; day <= days_in_month[month]; day++ )
  {
  cout << right << setw(2) << day;

  //Is day before Sat? Else start next line Sun.
  if ( ( day + daycode ) % 7 > 0 )
   cout << right << setw(3);
  else
  cout << "\n ";
  }
  // Set position for next month
  daycode = ( daycode + days_in_month[month] ) % 7;
 } 
}

 int main()
 {
 int year, daycode, leapyear;
 year = inputyear();
 daycode = determinedaycode(year);
 determineleapyear(year);
 calendar1(year, daycode);
 cout << "\n";
 
 return 0;
}
Topic archived. No new replies allowed.