Calendar Project (minor errors left)

Simply it doesn't align correctly can anyone help me I cant get it to work correctly. Also can you look over it to improve it in any way would be appreciated. Thanks in Advance!

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
169
170
171
#include <iostream>
using namespace std;
#include <iomanip>
#include <cassert>

int getYear()
{
   int year = 0;

   cout << "Enter year: ";
   cin >> year;

   while (year < 1753)
   {
      cout << "Year must be 1753 or later.\n"
           << "Enter year: ";
      cin >> year;
   }
   return year;
}

int getMonth()
{
   int month = 0;

   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;
}

bool isLeapYear(int year)
{
   if (year % 4 != 0)
      return false;

   if (year % 400 == 0)
      return true;

   return (year % 100 != 0);
}

int numDaysInMonth(int year, int month)
{
   if (month == 1 || month == 3 || month == 5 || month == 7 || month ==
       8 || month == 10 || month == 12)
      return 31;

   if (month == 4 || month == 6 || month == 9 || month == 11)
      return 30;

   if (month == 2)
   {
      if (isLeapYear(year))
         return 29;
      else
         return 28;
   }
}

int numDaysInYear(int year)
{
   if (isLeapYear(year))
      return 365;
   else
      return 365;
}

int computeOffset(int year, int month)
{
   int numDays=0;

   for (int count = 1753; count < year; count++)
      numDays += numDaysInYear(count);

   for (int count =1; count < month; count++)
      numDays += numDaysInMonth(year, count);

   int offset = numDays % 7;

   return offset;

}

void displayTable(int numDays, int offset)
{
   int dow = 1;

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

   if (offset == 6)
      offset = -1;

   for (int count =0; count <= offset; count++)
   {
      cout << "    ";
      dow++;
   }

   for (int count = 1; count <= numDays; count++)
   {
      cout << setw(2) << count;

      if (dow % 7 == 0 && count < numDays)
         cout << endl << "    ";
      else if (count < numDays)
         cout << "    ";
      else
         cout << endl;

      dow++;
   }
}

void displayHeader(int year, int month)
{
   if (month == 1)
      cout << "January, ";
   else if (month == 2)
      cout << "February, ";
   else if (month == 3)
      cout << "March, ";
   else if (month == 4)
      cout << "April, ";
   else if (month == 5)
      cout << "May, ";
   else if (month == 6)
      cout << "June, ";
   else if (month == 7)
      cout << "July, ";
   else if (month == 8)
      cout << "August, ";
   else if (month == 9)
      cout << "September, ";
   else if (month == 10)
      cout << "October, ";
   else if (month == 11)
      cout << "November, ";
   else 
      cout << "December, ";

   cout << year << endl;

}

void display(int year,int month, int offset)
{
   int numDays = numDaysInMonth(year, month);
   displayHeader(year, month);
   displayTable(numDays, offset);
}

int main()
{
   int month = getMonth();
   int year = getYear();
   cout << endl;
   int offset = computeOffset(year, month);

   display(year, month, offset);

   return 0;
}
Last edited on
You may need to include some other stream manipulator like right and left. Check: http://www.cplusplus.com/reference/iomanip/setw/

Aceix.
any other suggestions
@dagurr

You just have too many spaces in your couts in the displayTable function. Replace yours with this one, and everything lines up correctly when run.

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
void displayTable(int numDays, int offset)
{
	int dow = 1;

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

	if (offset == 6)
		offset = -1;

	for (int count = 0; count <= offset; count++)
	{
		cout << "    ";
		dow++;
	}

	for (int count = 1; count <= numDays; count++)
	{
		cout << setw(2) << count;

		if (dow % 7 == 0 && count < numDays)
			cout << endl << "  ";
		else if (count < numDays)
			cout << "  ";
		else
			cout << endl;

		dow++;
	}
}
@dagurr

Found one small error in your program when calculating number of days in year. You return 365 no matter if it's a leap year or not, instead of 366 if it is.
Topic archived. No new replies allowed.