C++ code for a calendar -- Help with a counter?

In a calendar program, how to figure out which day a month ends on in order to start the next month on the right day?

Hi! I was wondering if someone could help me to figure out how to solve a problem in my code. It's the first semester I take C++ programming, so I'm still struggling with the design of the programs.

I need to write the C++ code for a calendar that only requires the user to enter the year and in which day the year starts (Sunday, Monday, etc). This code has to be able to handle leap years (by diving the year by 400, or by 4 or 100), and show the twelve months. This is the code I've written so far. It recognizes if the year is leap or odd, and prints me the twelve months in the desired way. However, I still can't figure out how to make the program able to recognize in which day a month ends in order to start the next month in the right day. I hope you can help me with that. Any hint will be highly appreciated.

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

int main ()
{
	int year, start_day, type, n_days, month(1);
	cout << "Enter the year you want the program to display. ";
	cin >> year;
	if ((year%400==0) || ((year%100==0) ^ (year%4==0)))
	{
		type = 1;
		cout << "\nThe year is leap year.";
	}
	else
	{
		type = 0;
		cout << "\nThe year is odd year.";
	}
	cout << "\nEnter the first day of January: 1 for Sunday, 2 for Monday, etc. ";
	cin >> start_day;
	while (start_day < 0 || start_day > 12)
	{
		cout << "\nThe number entered is invalid.\nPlease enter a number between 1 and 7. ";
		cin >> start_day;
	}
	
	while (month <= 12)
	{
	switch (month)
	{
		case 1:
			cout << "\n\nJanuary\n" << endl;
			n_days = 31;
			break;
		case 2:
			cout << "\n\nFebruary\n" << endl;
			if (type = 1)
			{
				n_days = 29;
			}
			else
			{
				n_days = 28;
			}
			break;
		case 3:
			cout << "\n\nMarch\n" << endl;
			n_days = 31;
			break;
		case 4:
			cout << "\n\nApril\n" << endl;
			n_days = 30;
			break;
		case 5:
			cout << "\n\nMay\n" << endl;
			n_days = 31;
			break;
		case 6:
			cout << "\n\nJune\n" << endl;
			n_days = 30;
			break;
		case 7:
			cout << "\n\nJuly\n" << endl;
			n_days = 31;
			break;
		case 8:
			cout << "\n\nAugust\n" << endl;
			n_days = 31;
			break;
		case 9:
			cout << "\n\nSeptember\n" << endl;
			n_days = 30;
			break;
		case 10:
			cout << "\n\nOctober" << endl;
			n_days = 31;
			break;
		case 11:
			cout << "\n\nNovember" << endl;
			n_days = 30;
			break;
		case 12:
			cout << "\n\nDecember\n" << endl;
			n_days = 31;
			break;
		default:
			;
	}
	
	cout << endl << "Sun\tMon\tTue\tWed\tThr\tFri\tSat\n"; 
	for (int i = 1; i < start_day; i++) 
	{ 
		cout << " \t"; 
	} 
	for (int j = 1; j <= n_days; j++) 
	{ 
	if (((j + start_day - 2) % 7 == 0) && (j != 1)) 
		cout << endl; 
		cout << j  << "\t"; 
	} 
	cout << endl << endl; 

	month ++;

	}


	return 0;
}
On line 10: ^ does not what you think it does, it's a bitwise operator. See

http://www.cplusplus.com/doc/tutorial/operators/

On line 22: 12 -> 7

I suggest that you turn n_days into an array with the default values of the months. Then all you have to do on line 12 is to modify the second value.

on line 98: you need the sum of the month days and this is where you need the modulo of
Topic archived. No new replies allowed.