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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130`` ``````#include #include 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?

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.