Making a calendar in C++

Hi,

I'm trying to make a calendar but it comes out wrong. This is the output I get:

             January 2009
Sun   Mon   Tue   Wed   Thu   Fri   Sat

 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
            February 2020
Sun   Mon   Tue   Wed   Thu   Fri   Sat

 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


Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Calendar.h
#include "weekDay.h"
#include "exDate.h"
#include <string>

class Calendar
{
private:
	int month;
	int year;
	int firstday;
public:
	Calendar(int =1, int =1500);
	void setFirstDay();
	void print();
};


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
//Calendar.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include "Cal.h"

Calendar :: Calendar (int _month, int _year){

	month = _month;
	year = _year;
}

void Calendar :: setFirstDay(){
//This part determines which day will be the first day of that year (0 for Sunday, 1 for Monday, etc.)
	int day=1;
	int y = year - (14-month)/12;
	int m = month +12 * ((14-month) / 12) -2;
	firstday= (day + y + y / 4 - y / 100 + y / 400 + (31 * m / 12)) % 7;
}

void Calendar :: print(){

	int NumberOfDaysInMonth;
	int FirstDayOfMonth = 0;
	int DayOfWeekCounter = 0;
	int DateCounter = 1;

	switch (month)
	{
	  case 1:
		cout<<setw(21)<<"January "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	  case 2:
		cout<<setw(21)<<"February "<<year;
		if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
		  NumberOfDaysInMonth = 29;
		else
		  NumberOfDaysInMonth = 28;
	  break;
	  case 3:
		cout<<setw(21)<<"March "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	  case 4:
		cout<<setw(21)<<"April "<<year;
		NumberOfDaysInMonth = 30;
	  break;
	  case 5:
		cout<<setw(21)<<"May "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	  case 6:
		cout<<setw(21)<<"June "<<year;
		NumberOfDaysInMonth = 30;
	  break;
	  case 7:
		cout<<setw(21)<<"July "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	  case 8:
		cout<<setw(21)<<"August "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	  case 9:
		cout<<setw(21)<<"September "<<year;
		NumberOfDaysInMonth = 30;
	  break;
	  case 10:
		cout<<setw(21)<<"October "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	  case 11:
		cout<<setw(21)<<"November "<<year;
		NumberOfDaysInMonth = 30;
	  break;
	  case 12:
		cout<<setw(21)<<"December "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	}

//Display the days at the top of each month

	cout<<"\nSun   Mon   Tue   Wed   Thu   Fri   Sat";
	cout<<"\n\n"<<setw(2);

//Determine where the first day begins

	for (FirstDayOfMonth; FirstDayOfMonth < firstday; ++FirstDayOfMonth)
	{

		cout<<setw(6);
	}

	int tempfirstday=firstday;
	DateCounter = 1;
	DayOfWeekCounter = tempfirstday;
//This loop represents the date display and will continue to run until 
//the number of days in that month have been reached
	for (DateCounter; DateCounter <= NumberOfDaysInMonth; ++DateCounter)
	{
		cout<<DateCounter<<setw(6);
		++DayOfWeekCounter;
		if (DayOfWeekCounter > 6)
		{
			cout<<"\n\n"<<setw(2);
			DayOfWeekCounter = 0;
		}
	}
	cout << " \n" ;

	tempfirstday = DayOfWeekCounter + 1;
}


And in the main it just creates two calendar objects, one for January 2009, the other for February 2020 and prints them.

Any ideas?
Last edited on
I edited your code a little bit, to be able to run it on my own PC.. Here, I've changed a few lines...


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
//Calendar.h
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<iomanip.h>

class Calendar
{
private:
	int month;
	int year;
	int firstday;
public:
	Calendar(int =11, int =2011);
	void setFirstDay();
	void print();
};




//Calendar.cpp

Calendar :: Calendar (int _month, int _year){

	month = _month;
	year = _year;
}

void Calendar :: setFirstDay(){
//This part determines which day will be the first day of that year (0 for Sunday, 1 for Monday, etc.)
	int day=1;
	int y = year - (14-month)/12;
	int m = month +12 * ((14-month) / 12) -2;
	firstday= (day + y + y / 4 - y / 100 + y / 400 + (31 * m / 12)) % 7;
}

void Calendar :: print(){

	int NumberOfDaysInMonth;
	int FirstDayOfMonth = 0;
	int DayOfWeekCounter = 0;
	int DateCounter = 1;

	switch (month)
	{
	  case 1:
		cout<<setw(21)<<"January "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	  case 2:
		cout<<setw(21)<<"February "<<year;
		if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
		  NumberOfDaysInMonth = 29;
		else
		  NumberOfDaysInMonth = 28;
	  break;
	  case 3:
		cout<<setw(21)<<"March "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	  case 4:
		cout<<setw(21)<<"April "<<year;
		NumberOfDaysInMonth = 30;
	  break;
	  case 5:
		cout<<setw(21)<<"May "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	  case 6:
		cout<<setw(21)<<"June "<<year;
		NumberOfDaysInMonth = 30;
	  break;
	  case 7:
		cout<<setw(21)<<"July "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	  case 8:
		cout<<setw(21)<<"August "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	  case 9:
		cout<<setw(21)<<"September "<<year;
		NumberOfDaysInMonth = 30;
	  break;
	  case 10:
		cout<<setw(21)<<"October "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	  case 11:
		cout<<setw(21)<<"November "<<year;
		NumberOfDaysInMonth = 30;
	  break;
	  case 12:
		cout<<setw(21)<<"December "<<year;
		NumberOfDaysInMonth = 31;
	  break;
	}

//Display the days at the top of each month

	cout<<"\nSun   Mon   Tue   Wed   Thu   Fri   Sat";
	cout<<"\n\n"<<setw(2);

//Determine where the first day begins

	for (FirstDayOfMonth; FirstDayOfMonth < firstday; ++FirstDayOfMonth)
	{

		cout<<setw(14);
	}

	int tempfirstday=firstday;
	DateCounter = 1;
	DayOfWeekCounter = tempfirstday;
//This loop represents the date display and will continue to run until
//the number of days in that month have been reached
	for (DateCounter; DateCounter <= NumberOfDaysInMonth; ++DateCounter)
	{
		cout<<DateCounter<<setw(6);
		++DayOfWeekCounter;
		if (DayOfWeekCounter > 6)
		{
			cout<<"\n\n"<<setw(2);
			DayOfWeekCounter = 0;
		}
	}
	cout << " \n" ;

	tempfirstday = DayOfWeekCounter + 1;
}

void main()                    //This was to just be able to run your class...
{
	Calendar c;
	clrscr();
	c.setFirstDay();
	c.print();
	getch();
}


I changed line 110, from setw(6) to setw(14)...
and I got a decent output for November 2011. Perfect alignment. Try it out...

EDIT: Ohk, I found the problem. The thing is, depending on what the first day is (Monday, Tuesday.....), the setw has to be changed. So you'll have to experiment a little bit to find out...
Last edited on
if your really interest in Building Calender Application then you can check it out.....
http://programming-technique.blogspot.com/2011/09/mini-project-calendar-application-using.html
Topic archived. No new replies allowed.