Generating Yearly Calender

closed account (Diz60pDG)
So I am writing a code to generate a yearly calender based on the year put it in and the day of the week January starts on, and it takes into acoount whether it is a leap year or not

Right now I am able to print out the month of January, but I want to be able to print out all of the months, how would I go about doing this?

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

bool isLeapYear(int year);
int daysInMonth(int month, bool lpYear);
int printCalendar(int month);
int printDay(int dow);
string daysofweek = "Su Mo Tu We Th Fr Sa";
int month = 1; 			
int maxdaysinmonth = 0; 	
int endofweek = 6;  		
int startingspaces = 0; 	
int daycount = 1;  		


bool isLeapYear(int year)
{
	return ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0));
}

int main()
{
	int dow, year;
	cout << "Calender Program" << endl;
	cout << "What year is it?";
	cin >> year;
	cout << "What day of the week does January start on?" << endl;
	cout << "Enter 0 for Sunday, 1 for Monday...6 for Saturday" << endl;
	cin >> dow;

	

	if (month == 1)
	{
		cout << "      January" << endl;
		maxdaysinmonth = 31;
	}
	
	


	if (dow == 0)
	{
		startingspaces = 1;
	}
	else if (dow == 1)
	{
		startingspaces = 4;
	}
	else if (dow == 2)
	{
		startingspaces = 7;
	}
	else if (dow == 3)
	{
		startingspaces = 10;
	}
	else if (dow == 4)
	{
		startingspaces = 13;
	}
	else if (dow == 5)
	{
		startingspaces = 16;
	}
	else if (dow == 6)
	{
		startingspaces = 19;
	}

	for (int i = 1; i <= startingspaces; i++)
	{
		cout << " ";
	}

	while (daycount < 10)
	{
		if (daycount < 9)
		{
			cout << daycount << "  ";
		}
		else
		{
			cout << daycount << " ";
		}
		daycount++;
		if (dow == 6)
		{
			cout << endl << " ";
			dow = 0;
		}
		else
		{
			dow++;
		}
	}

	while (daycount <= maxdaysinmonth)
	{
		cout << daycount << " ";
		daycount++;
		if (dow == 6)
		{
			cout << endl;
			dow = 0;
		}
		else
		{
			dow++;
		}
	}

	month++;
	cout << endl;


	return 0;
}
Whenever you find yourself repeating something, make a function and call it in a loop.

Now, there is a trick to the "make a calendar" program that trips people up. You can't just cout to the console right away if you want to line up three months in four rows.

If you want to do it that way then you'll need to have a 2D array of characters as wide as three months and as tall as one month (counting the worst case).

Then your 'print month' function must also take as argument a column offset into the array. Once you have three months, cout your array and go on to the next three months.

Don't let it overwhelm you. Get your program working just printing a single month per row, then backup your working program, then update the program do do as above.

Hope this helps.

[edit] I can't remember the link, but there's a FAQ for handling dates and time. When I get home later tonight I'll update for you.
Last edited on
Topic archived. No new replies allowed.