Making a Calendar in C++

Any ideas of how I can have a calendar for all 12 months print for any year?
The user inputs the year and the day of the week for January 1st of that year. (Sunday = 0, Monday = 1, etc.)


Could someone provide a pseudocode or any suggestions?
Last edited on
You could have a system like this:

enum Month{All = days, Of = days, the = days, Months = days}

Find Starting Point

Recursive For Loop
-Print
-Call Apon Self

If the Days in the Month enum is at the max given then Month = Next in the sequence
I couldn't figure out how to do what you were talking about, lol. But I made progress:

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




int main()
{
int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int DaysinWeek = 0;
int year;
int day;
int FirstDayOfMonth = 0;
int month = 1;



cout << "Calendar Program" << endl << endl;
cout << "Please enter the year: ";
cin >> year;
if (year < 0)
{cout << "The year must be greater than or equal to 0!" << endl;
return 0;}
cout << "Please enter the day of the week of January 1st." << endl;
cout << "Enter 0 for Sunday, 1 for Monday, ..., 6 for Saturday: ";
cin >> day;
if (day < 0 || day > 6)
{cout << "The day of January 1st must be between 0 and 6!" << endl;
return 0;}

cout << endl << endl;






// Print this year's calendar
for (int month = 0; month < 12; ++month) {
{
	
for (int i = 0; i < DaysinWeek; ++i)
{cout << setw(3) << " ";}


if (month == 0)
{cout << "January \n";}

if (month == 1)
{cout << "February \n";
 if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
mdays[1] = 29;}

if (month == 2)
{cout << "March \n";}

if (month == 3)
{cout << "April \n";}

if (month == 4)
{cout << "May \n";}

if (month ==5)
{cout << "June \n";}

if (month == 6)
{cout << "July \n";}

if (month == 7)
{cout << "August \n";}

if (month == 8)
{cout << "September \n";}

if (month == 9)
{cout << "October \n";}

if (month == 10)
{cout << "November \n";}

if (month == 11)
{cout << "December \n";}

}


//spaces
for (int i = 0; i < day; i++)
	{cout << "  ";
}

  //Determine where the first day begins
day = 0;
    for (FirstDayOfMonth; FirstDayOfMonth < day; ++FirstDayOfMonth)
    {   
        cout << setw(3);
    } 

   






// Print this month's calendar
for (int mday = 0; mday < mdays[month]; ++mday) {
cout << setw(3) << mday + 1;
DaysinWeek++;
if (DaysinWeek == 7) {
cout << "\n" << setw(3);
DaysinWeek = 0;
}

}

// Set up for the next month
if (DaysinWeek != 7) {
cout << "\n";
}

cout << endl;
day = DaysinWeek + 1;
}



return 0;
}



This is what I get: http://i.imgur.com/9ypos.jpg
Can someone please tell me how to get the month names all aligned to the left? And how to fix the alignment of the first week?
http://zanasi.chem.unisa.it/download/C.pdf


If you check this out, there is a C program that gives some clues about this.

One is to use 2 arrays, one for days in month, the other for month names. that makes it easier to look up the month name, without lots of if else clauses.

When I did this years ago, I remember there being a formula that calculated which day of the week it was given the day, month & year. It calculated the number of days since sometime in the 1700's (1752? a guess) , which was the last time that anyone meddled with the calendar. It wasn't a straight forward formula, our teachers gave it to us as part of the assignment.

I guess you could do the same thing, start from a date that you know which day it was, calc the number of days to the input date, use the % operator (modulus) to work out which day it is.

On my linux system, this is what I get for Jan 1800:

    January 1800    
Su Mo Tu We Th Fr Sa
          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



    January 1900    
Su Mo Tu We Th Fr Sa
    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




And Jan 2000:

    January 2000    
Su Mo Tu We Th Fr Sa
                   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




Your program will be better if it can handle a wide range of dates - you can use the jan 1900 & 2000 to see that your day of week calc works.

Hope all goes well




Last edited on
Topic archived. No new replies allowed.