Calender Program

Pages: 12
closed account (oG8qGNh0)
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
#include <iostream>
#include <iomanip>
using namespace std;

typedef int Bool;

// Converts 0 to S , 1 to M , etc
char whatDay (int);

// Check for leap years
Bool isLeapYear (int);

// Takes the number of the month, a flag saying whether or not it is a leap year
int numOfDaysInMonth (int,Bool);

void printHeader (int);

// Takes the number of the month and the first day, prints, and updates the first day of the next month
void printMonth (int, int&);

// Prints the needed number of spaces
void skip (int);

// Prints leading spaces in monthly calendar
void skipToDay (int);

// Terminates program incase of errors
void disaster ();

int main () {

int year, firstDayInCurrentMonth;
Bool leap;
int currentMonth = 1; // Starts at January
int numDays;

cout << "What year?: ";
cin >> year;
cout << "On what day does January 1st fall? " << endl;
cout << "Enter (0 for Sunday, 1 for Monday, etc.) ";

leap = isLeapYear (year);
skip (9); cout << year << endl;

while (currentMonth <= 12) {
numDays = numOfDaysInMonth (currentMonth, leap);
printHeader (currentMonth);
printMonth (numDays, firstDayInCurrentMonth);
cout << endl << endl << endl;
currentMonth = currentMonth + 1;
}
cout << endl;
void disaster () {
cout << "Disaster: Exiting ..." << endl;
exit (-1);
}
}
void skip (int i) {
while (i > 0) {
cout << " ";
i = i - 1;
}
}
char whatDay (int d) {
if (d==0) return('S');
else if (d==1) return ('M');
else if (d==2) return ('T');
else if (d==3) return ('W');
else if (d==4) return ('T');
else if (d==5) return ('F');
else if (d==6) return ('S');
else disaster ();
}
Bool isLeapYear (int y) {
return ((y % 4) == 0); // Simplified
}
void printHeader (int m) {
if (m == 1) (skip(7); cout << "January" << endl; )
else if (m == 2) (skip(7); cout << "February" << endl; )
else if (m == 3) (skip(7); cout << "March" << endl; )
else if (m == 4) (skip(7); cout << "April" << endl; )
else if (m == 5) (skip(7); cout << "May" << endl; )
else if (m == 6) (skip(7); cout << "June" << endl; )
else if (m == 7) (skip(7); cout << "July" << endl; )
else if (m == 8) (skip(7); cout << "August" << endl; )
else if (m == 9) (skip(7); cout << "September" << endl; )
else if (m == 10) (skip(7); cout << "October" << endl; )
else if (m == 11) (skip(7); cout << "November" << endl; )
else if (m == 12) (skip(7); cout << "December" << endl; )
else disaster ();

cout << " Su M T W Th F Sa" << endl;
cout << "_________________" << endl;
}
int numOfDaysInMonth (int m, Bool leap) {
if (m == 1) return (31); 
else if (m == 2) if (leap) return (29); else return(28);
else if (m == 3) return(31);
else if (m == 4) return(30);
else if (m == 5) return(31);
else if (m == 6) return(30);
else if (m == 7) return(31);
else if (m == 8) return(31);
else if (m == 9) return(30);
else if (m == 10) return(31);
else if (m == 11) return(30);
else if (m == 12) return(31);
else disaster ();
}
void skipToDay (int d) {
skip(3*d);
}
void printMonth (int numDays, int& weekDay) {
int day = 1;
skipToDay(weekDay);
while (day <= numDays) {
cout << setw(2) << day << " ";
if (weekDay == 6 ) {
cout << endl;
weekDay = 0;
}
else weekDay = weekDay + 1;
day = day + 1;
}
}


Sorry About NOT using code tags... Error in lines 53 and 78. How can I make that disappear?
1. You used code tags. Good. However, codd tags preserve indentation, which makes the code much more readable. You evidently don't use indentation. You should, and your teacher should require it. If you want to make it easier for us to provide you with comments, please indent your code.

2.
Error in lines 53 and 78.
Well, what are they? Paste the error message here, too, and we can all read them.
@naveenmmenon
This is what indentation should look like.
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iostream>
#include <iomanip>
using namespace std;

typedef int Bool;

// Converts 0 to S , 1 to M , etc
char whatDay(int);

// Check for leap years
Bool isLeapYear(int);

// Takes the number of the month, a flag saying whether or not it is a leap year
int numOfDaysInMonth(int, Bool);

void printHeader(int);

// Takes the number of the month and the first day, prints, and updates the first day of the next month
void printMonth(int, int &);

// Prints the needed number of spaces
void skip(int);

// Prints leading spaces in monthly calendar
void skipToDay(int);

// Terminates program incase of errors
void disaster();

int main()
{

  int year, firstDayInCurrentMonth;
  Bool leap;
  int currentMonth = 1;         // Starts at January
  int numDays;

  cout << "What year?: ";
  cin >> year;
  cout << "On what day does January 1st fall? " << endl;
  cout << "Enter (0 for Sunday, 1 for Monday, etc.) ";

  leap = isLeapYear(year);
  skip(9);
  cout << year << endl;

  while (currentMonth <= 12) {
    numDays = numOfDaysInMonth(currentMonth, leap);
    printHeader(currentMonth);
    printMonth(numDays, firstDayInCurrentMonth);
    cout << endl << endl << endl;
    currentMonth = currentMonth + 1;
  }
  cout << endl;
  void disaster() {
    cout << "Disaster: Exiting ..." << endl;
    exit(-1);
  }
}

void skip(int i)
{
  while (i > 0) {
    cout << " ";
    i = i - 1;
  }
}

char whatDay(int d)
{
  if (d == 0)
    return ('S');
  else if (d == 1)
    return ('M');
  else if (d == 2)
    return ('T');
  else if (d == 3)
    return ('W');
  else if (d == 4)
    return ('T');
  else if (d == 5)
    return ('F');
  else if (d == 6)
    return ('S');
  else
    disaster();
}

Bool isLeapYear(int y)
{
  return ((y % 4) == 0);        // Simplified
}

void printHeader(int m)
{
  if (m == 1)
    (skip(7);
     cout << "January" << endl;
      )
      else
if (m == 2)
  (skip(7);
   cout << "February" << endl;
      )
      else
if (m == 3)
  (skip(7);
   cout << "March" << endl;
      )
      else
if (m == 4)
  (skip(7);
   cout << "April" << endl;
      )
      else
if (m == 5)
  (skip(7);
   cout << "May" << endl;
      )
      else
if (m == 6)
  (skip(7);
   cout << "June" << endl;
      )
      else
if (m == 7)
  (skip(7);
   cout << "July" << endl;
      )
      else
if (m == 8)
  (skip(7);
   cout << "August" << endl;
      )
      else
if (m == 9)
  (skip(7);
   cout << "September" << endl;
      )
      else
if (m == 10)
  (skip(7);
   cout << "October" << endl;
      )
      else
if (m == 11)
  (skip(7);
   cout << "November" << endl;
      )
      else
if (m == 12)
  (skip(7);
   cout << "December" << endl;
      )
      else
  disaster();

  cout << " Su M T W Th F Sa" << endl;
  cout << "_________________" << endl;
}

int numOfDaysInMonth(int m, Bool leap)
{
  if (m == 1)
    return (31);
  else if (m == 2)
    if (leap)
      return (29);
    else
      return (28);
  else if (m == 3)
    return (31);
  else if (m == 4)
    return (30);
  else if (m == 5)
    return (31);
  else if (m == 6)
    return (30);
  else if (m == 7)
    return (31);
  else if (m == 8)
    return (31);
  else if (m == 9)
    return (30);
  else if (m == 10)
    return (31);
  else if (m == 11)
    return (30);
  else if (m == 12)
    return (31);
  else
    disaster();
}

void skipToDay(int d)
{
  skip(3 * d);
}

void printMonth(int numDays, int &weekDay)
{
  int day = 1;
  skipToDay(weekDay);
  while (day <= numDays) {
    cout << setw(2) << day << " ";
    if (weekDay == 6) {
      cout << endl;
      weekDay = 0;
    } else
      weekDay = weekDay + 1;
    day = day + 1;
  }
}


First problem, just trying to indent it.

$ indent -kr -nut -ts2 -i2 -l120 foo.cpp
indent: foo.cpp:79: Error:Unmatched 'else'
indent: foo.cpp:80: Error:Unmatched 'else'
indent: foo.cpp:81: Error:Unmatched 'else'
indent: foo.cpp:82: Error:Unmatched 'else'
indent: foo.cpp:83: Error:Unmatched 'else'
indent: foo.cpp:84: Error:Unmatched 'else'
indent: foo.cpp:85: Error:Unmatched 'else'
indent: foo.cpp:86: Error:Unmatched 'else'
indent: foo.cpp:87: Error:Unmatched 'else'
indent: foo.cpp:88: Error:Unmatched 'else'
indent: foo.cpp:89: Error:Unmatched 'else'
indent: foo.cpp:90: Error:Unmatched 'else'

Look carefully, in every statement block of printHeader, you've used ( ) instead of { } to denote the statement block of each conditional.

Also look at line 55.
Do you see that your disaster function is actually scoped within main().

You NEED to slow down and compile much more often than you're apparently doing.
For example.
1
2
3
4
5
6
7
void printHeader(int m)
{
  if (m == 1)
    (skip(7);
     cout << "January" << endl;
      )
}

This should have been a point you should have compiled and tested the code to make sure it's what you want.

Copy/pasting the same mistake a dozen times just means you have to fix 12 things rather than one thing, when you finally discover that your original edit was wrong.





Topic archived. No new replies allowed.
Pages: 12