A program that, given a month and a year, outputs a calendar for that month

i need help, been trying to figure this out. i am beginner in c++ , i need to write a programm that out ouputs a calender for a month when given a month and year using this frame work :

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>

using namespace std;


// returns true if the year is a leap year, and false otherwise
// Hint: a year is a leap year if it's divisible by 4 unless it's
// divisible by 100, with the exception being that years divisible
// by 400 are leap years!
bool isLeap (int year);


// returns the number of days in the specified month and year
// Hint: use isLeap if the month is February
int daysInMonth (int month, int year);


// returns number of preceding days in same year:
// 0 for Jan 1, 1 for Jan 2, and so on
// Hint: use daysInMonth
int daysSinceJan1 (int day, int month, int year);


// returns the number of days before Jan 1st of the specified year
// (using Jan 1 1900 as the starting point)
// returns 0 for 1900,
// 365 for 1901 (note: 1900 was NOT a leap year)
// 730 for 1902
// etc.
// Hint: use isLeap to help figure out how many days in each year
int daysInPreviousYears (int year);


// returns the number of days since Jan 1 1900
// Hint: use daysInPreviousYears and daysSinceJan1
int daysSinceStartOfTime (int day, int month, int year);


// returns the day of the week: 1 is Monday, 2 is Tuesday, etc.
// Hint: use daysSinceStartOfTime and Jan 1 1900 was a Monday
int dayOfWeek (int day, int month, int year);


// creates a calendar for the given month and year and prints
// it out nicely
void createCalendar (int month, int year) {

int startDay, days, count, i;

startDay = dayOfWeek (1, month, year);
days = daysInMonth (month, year);

cout << "\n Su Mo Tu We Th Fr Sa\n";

// work out how many blank entries should precede the first number
count = startDay % 7;
// output the required blank entries
for (i = 1; i <= count; i++) {
cout << setw(4) << " ";
}

// output all of the numbers
for (i = 1; i <= days; i++) {
cout << setw(4) << i;
count++;
if (count == 7) { // if the current output line has 7 items on it
cout << endl; // end the current output line
count = 0; // and begin a new one
}
}

if (count != 0) { // if we're part way through an output line
cout << endl; // end this output line
}

cout << endl;

} // end createCalendar


int main (void) {

int month, year;

// get month, year pairs from the user until 0 0 entered
for (;;) {

cout << "Enter month (1 - 12), and year (>= 1900)." << endl
<< "Use two zeroes to terminate program." << endl << ": ";
cin >> month >> year;

if (cin.fail()) { // user entered something other than numbers

// Here's how to get rid of crazy input so the program doesn't loop forever.
// We'll cover this in Chapter 8.
cin.clear(); cin.ignore (INT_MAX, '\n');
cout << "Garbage input ignored.\n";

} else {

if ((month == 0) && (year == 0)) { // sentinel values

break;
}

if (month < 1 || month > 12 || year < 1900) { // invalid values

cout << "Invalid values ignored.\n";

} else { // valid month and year

createCalendar (month, year);

} // end if

} // end if

} // end for (;;)

system ("PAUSE"); return 0;

} // end main
You should go and congratulate your professor or tutor for doing such a wonderful job in developing this comprehensive pseudo code. Write the code to do one thing at a time so that you can test it (and correct it where necessary) before moving on to the next task. For example write the code (main() and the function to get the year from the user and determine if it is a leap year. Post your code snippet here to get assistance when needed. Go onto the next task.
Also, remember to post your code with code tags - select your code, press the <> button on the right under the Format: menu, so it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>

using namespace std;


// returns true if the year is a leap year, and false otherwise
// Hint: a year is a leap year if it's divisible by 4 unless it's
// divisible by 100, with the exception being that years divisible
// by 400 are leap years!
bool isLeap (int year);


Doing this preserves the formatting, and shows line numbers - which makes it a lot easier for us to read & help you.

You can also use the quote, bold, underline, Italics etc facilities.

If you have any compile errors, post these in full including the line numbers as well.

Hope all goes well :+D
If that's all it has to do I'd just do a buttload of if-else statements that calculate everything based on a fixed date, maybe throw a few loops in there. But what do I know I'm a noob.

Plus just using if-else over and over is no way to learn.
- My algorithm would simply check if the year was a leap year or not, and i would have them enter a number from 1-12 for the month and display the days in the months that way.
@thejman250

That's not really enough - the key to it is figuring out which day the month starts on. To do this one needs to calculate how many days since 1 Jan 1900 (For this assignment). There are several hints about this in the assignment.

@mary2

buffbill is right the assignment explains what to do pretty well.

Good Luck.
here i create this calendar but this code is mess
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
using namespace std;
class date{

private:
	int day;
	int month;
	int year;
	static int days_in_month[];
	bool leapyear(int);
public:
	date(int u=0,int day=1,int month=1,int year=2012);
	void setdate(int,int,int,int);
 	void display();



};
void date::display()
	{
		cout<<day<<"-"<<month<<"-"<<year<<endl;
		cout<<endl;
		cout<<endl;
	}
int date::days_in_month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
date::date(int u,int d,int m,int y)
{
	cout<<"Current Date"<<endl;
	cout<<endl;
	cout<<endl;
	cout<<'\t'<<d<<'\t'<<m<<'\t'<<y<<endl;
	cout<<endl;
	cout<<endl;
	cout<<"Days You want To jump IS = "<<u<<endl;
	cout<<endl;
	cout<<endl;
	setdate(u,d,m,y);

}
void date::setdate(int u,int d,int m,int y)
{
	if(d>31)
	{
		cout<<"wrong day :"<<d<<": i'm setting to 31 "<<endl;
		cout<<endl;
		cout<<endl;
		d = 31;
		
	}
    year = y;
	if((m>=1) && (m<=12))
	{
		month = m;
	}
	else
	{
		cout<<"month IS incorrect so i'm setting it to 1"<<endl;
		cout<<endl;
		cout<<endl;
		month = 1;
	}
	if(m == 4 || m == 5 || m == 9 || m == 11)
	{
		if(d>30)
		{
		cout<<"The month "<<m<<" cannot have 31 days so i'm setting it to 30"<<endl;
		cout<<endl;
		cout<<endl;
		d = 30;
		}

	}
	if(d<=31)
	{
	
		
			for(int i =1;i<=u;i++)
			{
				if(  month == 2 && leapyear(y))
				{
					
					if(d> 29 || d== 28)
					{
						cout<<"The month is feb and the year is leap So it cannot have :"<<d<<": days"<<" I'm setting it to 29"<<endl;
						cout<<endl;
		                cout<<endl;
						days_in_month[2] = 29;
					}
					days_in_month[2] = 29;
				}
				else
				{
					
					days_in_month[2] = 28;
				}
				
				
				if(d < days_in_month[month])
				{
					
					d++;
				}
				
				else
				{
			if(month >= 12)
			{
				year++;
				month = 0;
				
			}
			
			month++;
					d=0;
					d++;
				}
			}

			day = d ;
	}
	else
	{
		cout<<"day is incorrect so I'm setting it to 0;"<<endl;
		cout<<endl;
		cout<<endl;
		day = 0;
	}
}


bool date::leapyear(int y)
{
	if (y % 400 == 0 || y%100 != 0 && y%4 == 0) 
{
return true;
}
else
{
	return false ; 
}



}
int main()
{
	char c;
	do{
	int jump,day,month,year = 0;
	cout<<"Please enter the current date with spaces: ";
	cin>>day>>month>>year;
	cout<<"Please enter days you want to jump from the date you enter: "<<endl;
	cin>>jump;
	date d1(jump,day,month,year);
	d1.display();
	cout<<"do you want to continu: ";
	cin>>c;
	system("cls");
	}while(c == 'y' || c=='Y');
	
}
TheIdeasMan wrote:
@thejman250

That's not really enough - the key to it is figuring out which day the month starts on. To do this one needs to calculate how many days since 1 Jan 1900 (For this assignment). There are several hints about this in the


- Calculating how many days there are between the given date, and January 1st,1990 sounds pretty straight forward to me.
Last edited on
Thank you everyone for your contribution, i really appreciate, am working on the programm now. I hope it works out :)
thanks for your help
Topic archived. No new replies allowed.