### 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:

 ``12345678910111213`` ``````#include #include #include #include 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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161`` ``````#include 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<=1) && (m<=12)) { month = m; } else { cout<<"month IS incorrect so i'm setting it to 1"<30) { cout<<"The month "< 29 || d== 28) { cout<<"The month is feb and the year is leap So it cannot have :"<= 12) { year++; month = 0; } month++; d=0; d++; } } day = d ; } else { cout<<"day is incorrect so I'm setting it to 0;"<>day>>month>>year; cout<<"Please enter days you want to jump from the date you enter: "<>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 :)