Help With Error

Hi,
My C++ code is supposed to print a calendar for a user inputted year. The bsic functionality is done, and I was working on formatting when I rebuilt and ran it and now it does this:
1. ask for year (correct)
2. runs (really really fast)
3. has an error and puts up a ton of wierd characters.

At the time I was working on making the code shorter and better by changing out switch statements for for loops that pull from an array.

Has anyone seen anything like this before?
My code:
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
 #include <iostream>
 #include <stdlib.h>
 #include <ctype.h>
 #include <time.h>
 using namespace std;
 enum Month {Jan, Feb, Mar, Apr, May, Jun,
    Jul, Aug, Sep, Oct, Nov, Dec}; //defines month
 int getYear();			   // has the user enter a valid year
 bool isLeap(int year);	   // check for leap years
 void dayName();			  // prints the names for the day of the week
 void monthNameHeader(int year);  // puts head for the month name
 int startDay(int year);		  // decides what week day Jan starts on
 int monthCount(int counter);	 // how many days are in each month

 void newMonth(int startDOW);  // what day of the week new month starts on

 void printAll(int year);		 //  puts everything together, prints to screen


 int year = 0;				    // uesr inputed year || rand gen per 0
 int counter = 1;				 // counter for month name & # days in month
 int startDOW,				    // day of the week Jan starts on
     wrap,					    // check for if weekday is Saturday
     daysInMonth;				 // total days in each month
 int weekNumber = 0;			  // flag for first week of the month

 int main()
 {




 	year = getYear();		// has user enter year number
 	printAll(year);




 	  return 0;
 }


 int getYear()	   //prompts the user to enter a valid year
 {
 	char c;
 	srand(time(NULL));
 	cout << "Enter the year";
 	do {			  // gets whole number value
 		 cin.get(c);
 		 if(isdigit(c))
 		 {
 		  year=year*10;
 		  year +=(int)(c-'0');
 		 }
 	} while(c!='\n');
  return year;
 }


 bool isLeap(int year) // checking for possible leap year
 {
 	 if (year % 400 == 0)
 	 return true;
 	 if (year % 100 == 0)
 	 return false;
 	 if (year % 4 == 0)
 	 return true;
 	 return false;  // else return false
 }

 void dayName()
 {
    string day_str[7]={
	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
    cout << "  ";
    for(int i=0;i<7;i++){
    cout << day_str[i] << "  ";
    }
    cout << endl;
 	cout << "------------------------------------" << endl;
 }

 void monthNameHeader(int counter)
 {
        string month_str[12]={
	"Janunary", "Feburary", "March", "April",
	"May", "June", "July", "August",
	"September", "October", "November", "December"
};
        cout << "	"<<month_str[counter]<<" "  << endl;

}

 int monthCount(int counter)	  // how many days are in the month
 {
     int monthDays[12]={31,28,31,30,31,30,31,31,30,31,30,31};
         daysInMonth=monthDays[counter];
         return daysInMonth;

 }


 int startDay(int year)
 {

 	startDOW = (year + (year - 1 ) /4 - (year - 1) / 100 + (year - 1) /400) %7;
 	return startDOW;			   // formula for what DoWeek year starts on
 }


 void printAll(int year)
 {

 	 for (counter = 1; counter <= 12; counter++)
 	 {

 		  monthNameHeader(counter);			   // prints month day
 		  dayName();						   // prints the name of days
 		  if (counter==1)
 			  wrap = startDay(year) ;	// what day Jan starts on
 		  else
 			  startDOW = wrap;			   // what day other months start on

 		  cout << " ";

 		  for (int loopCount = 0; loopCount < startDOW; loopCount++)
 		  {
 				  cout << "   ";	   // how many space to indent new month
 		  }


 		  monthCount(counter);			 // how many days in month

 		  for (int dayCounter=1;dayCounter<=daysInMonth; dayCounter++)
 		  {

 				 if (wrap == 7)	   //if Saturday, carriage return
 				 {
 					 cout << "\n ";
 					 wrap = 0;		   //resets day of week counter
 					 weekNumber++;	   //no longer first week of month
 				 }
 				 if (dayCounter<10)	  //adds space for single digit days
 					 cout << "   ";
 				 cout << dayCounter << "   ";   //prints the day #
 				 wrap++;
 		  }

   //	cout << "\nthis month starts on day number " << startDOW;  *testing*
   //	cout << "\ndays in this month are " << daysInMonth;	    *testing*
 	  cout << "\n\n";
 	  cout << endl;
 	  }  // end BIG for loop


 }
Last edited on
Fixed it. I forgot when I swapped in arrays for cases to use months-1 and days-1 since arrays start at position 0.
Topic archived. No new replies allowed.