problem with my calendar

My program has the problem where after it displays the regular months (1-12) it then displays more months after that. Also the start days always start on a Tuesday.

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
  #include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

void printmonth(const int month, const int startday, const bool leap);
void printyear(int year);
bool intleapyear(int year);

int main(void)
{
	int year;

	cout << " Please enter a year equal or greater than 1582: ";
	cin >> year;

	while(year < 1582 )
	{
		cout << "Invalid Imput\n";
		cout << "Please enter a valid year: ";
		cin >> year;
	}
	printyear(year);
	 

	printmonth(1,0,false); // Print January 1st, on a Sunday
	printmonth(2,1,true); // Print February 1st leap year, on Monday
	printmonth(1,2,false); // Print January 1st, on a Tuesday
	printmonth(2,3,false); // Print February 1st not leap, on Wednesday
	printmonth(1,4,false); // Print January 1st, on a Thursday
	printmonth(2,5,false); // Print February 1st, on a Friday
	printmonth(1,6,false); // Print January 1st, on a Saturday
	printmonth(6,1,false); // Print June 1st, on Monday
	printmonth(12,4,false); // Print December 12th, on a Thursday

	return 0;
}

bool isleapyear(int year)
{
	if(year%4 == 0)
	{
		return true;
	}
	else 
	{
		return false; 
	}
}

void printmonth(const int month, const int startday, const bool leap)
{
	ofstream myfile ( "cal.dat", ios::app );

	//Unable to use constants so declared new variables with their values
	//Flag is used to keep track of where the first day is printed and every new line after
	int i, flag = 1, real1 = month, real2 = startday;
	//Standard arrays containing the name of the months and their respective days in the month with a leap year being the last number in the array
	string m[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "Ocotober", "November", "December"};
	int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 29};
	//Used to determine how many spaces to start off the line
	int start[] = {3, 7, 11, 15, 19, 23, 27};
	
	//Checks if it's February and a leap year
	if(month == 2 && leap == true)
		real1 = 13;
	
	//Standard printing before the days
	myfile << "          " << m[month - 1] << endl;
	myfile << "Sun Mon Tue Wed Thu Fri Sat" << endl;
	//Prints 7 days of the month on a line until the month is over
	for(i = 0; i < days[real1 - 1]; i++)
	{
		//Used to start off the printing process and every new line after
		if(flag == 1)
		{
			myfile << setw(start[real2]) << i + 1;
			flag = 0;
			//When it reaches the 7th position, reset the values for the start of a new line
			if(real2 == 6)
			{
				myfile << endl;
				real2 = 0;	
				flag = 1;
			}
			else
				real2 += 1;
		}
		else
			if(real2 == 6)
			{
				myfile << setw(4) << i + 1 << endl;
				real2 = 0;
				flag = 1;	
			}
			else
			{
				myfile << setw(4) << i + 1;
				real2 += 1;
			}
	}
	//Finishes the line if it is uncomplete
	if(real2 != 0)
		myfile << endl;
		
	myfile.close();
}

void printyear(int year)
{
	ofstream myfile ( "cal.dat", ios::app );

	myfile << "          " << year << endl << endl;

		int a, day, m, d, y, month; 

		a=(14-month)/12;
		y=year-a;
		m= month+12*a-2;
		d=(day+y+y/4-y/100 + y/400 + (31*m/12))%7;

	for(month = 1; month < 13; month++)
	{
		printmonth(month,d,isleapyear(year));
	}


	myfile.close();
}
Last edited on
Hi kaifeng08,

Some things I noticed:

Your isleapyear function is too simplistic. Every 4th year is a leap year, every 100 isn't, but every 400 is.

On line 119, month is not initialised, on line 122 variable day is not either. You should set your compiler warnings to their maximum to tell you about this. I use GCC with these warnings all the time:

-Wall -Wextra -pedantic

You are also doing integer division there : variable a will always be zero.

Uninitialised variables are one of the main causes of problems, - always initialise variables. I like to do it upon declaration where possible, if you do 1 per line then you have the opportunity to comment them as well, although you should try to have meaningful names so the program is self explanatory. Use the comments to mention other important things about the variable.

When I did this about 20 years ago, I remember the formula for calculating the day number being much more complicated than Yours. Your are doing integer division again big time.

Hope all goes well - I look forward to see how you get on. Cheers 8-)
Topic archived. No new replies allowed.