Printing out a Monthly Calendar

I need to write a program that only accepts a month and year between 1/1901 and 12/2099 and print out a monthly calendar (January 1, 1901 is a Tuesday). I know how factor in a leap year. What I'm having trouble with is starting off...

The output should be something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Enter a month and year between 1/1901 and 12/2099: 10/2014 (entered by user)

Calendar: October, 2014
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

Print a calendar for another month? (Y/N) y

Enter a month and year between 1/1901 and 12/2099: 2/2012 (entered in by user)
Calendar: February, 2012
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

Print a calendar for another month? (Y/N) n
Press any key to continue . . .


This is what I have so far (not complete), I realize there are some parts that do not make sense, those are the places I need the most help.

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
 #include <iostream>
 using namespace std;
 int main()
{
    string dayName = "Sun Mon Tue Wed Thu Fri Sat;
    int month, year;
    char ch;

    cout << "Enter a month and year between 1/1901 and 12/2099: ";
    cin >> month >> ch >> year;
    	if ((month > 1 && month < 12) && (year < 1901 && year > 2099)) true;
    		else cout << "Please enter a valid date.";		
	cout << endl;
	
    int firstDay;
    int day, Days, numDays;
    numDays = (year - 1901) * 365 + (year - 1901)/ 4;
	
	
	switch (month)
    {
       case 1: "January"
	   case 2: "Febuary"
	   		Days = (year % 4 == 0) ? 29: 28;
	   case 3: "March"
	   case 4: "April"
	   case 5: "May"
	   case 6: "June"
	   case 7: "July"
	   case 8: "August"
	   case 9: "September"
	   case 10: "October"
	   case 11: "November" 
	   case 12: "December"
	}
    firstDay = (2 + numDays) % 7; firstDay
	
    for (day = 1; day <= days; day++)
	{
		
    	if ((day + fisrtday) % 7 == 0)
    		cout << endl;
	}   
	cout << endl; 
	
	return numDays

    return 0;
} 

Is there any advice/hints that anyone can offer? It would be greatly appreciated.

P.S. My instructor wants the code to be efficient, hence it shouldn't exceed 25-35 lines of code, which I find difficult...
Last edited on
what is this? string dayName = "Sun Mon Tue Wed Thu Fri Sat;
What is this? numDays = (year - 1901) * 365 + (year - 1901)/ 4;
what is this? firstDay = (2 + numDays) % 7;
what are you doing?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch (month)
    {
       case 1: "January"
	   case 2: "Febuary"
	   		Days = (year % 4 == 0) ? 29: 28;
	   case 3: "March"
	   case 4: "April"
	   case 5: "May"
	   case 6: "June"
	   case 7: "July"
	   case 8: "August"
	   case 9: "September"
	   case 10: "October"
	   case 11: "November" 
	   case 12: "December"
	}


Algo:
1
2
3
4
get month and year
from month and year, get start day of month  //this is not that easy
from month, get number of days of month
prrint calender
@azl4182
I wrote a calendar program awhile back, and used this function to find the first day of any given month in a year
1
2
3
4
5
6
7
8
int FirstDayOfMonth(int year,int month)
{
	int t[12] = {0,3,2,5,0,3,5,1,4,6,2,4};
	int y = year - (month < 2);
	y=(y + y/4 - y/100 + y/400 + t[month]+1) % 7;
        // y = 0 for Sunday, 1 for Monday.. etc.
	return y;
}


And this let's you know if the year is a leap year or not.

1
2
3
4
5
6
7
8
9
10
11
int GetDaysInMonth(int year,int month)
{
	int The_months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
	// Days In Each Month;
	bool A_Leap_Year =  ((year%400 == 0) || (year%4==0 && year%100 !=0));

	if( A_Leap_Year)
		The_months[1] = 29;

	return The_months[month];
}


Hope this helps you out..
@whitenite
I also wrote that program about 2 years ago.
I used this function.
1
2
3
4
5
6
7
8
int day(int d,int m, int y)
{
/*The following, invented by Mike Keith and published in Journal of Recreational Mathematics,
 Vol. 22, No. 4, 1990, p. 280, 
 is conjectured to be the shortest expression of a day-of-the-week algorithm: */

	return  (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7 ;
}


Yours is,
1
2
3
4
5
6
7
8
int FirstDayOfMonth(int year,int month)
{
	int t[12] = {0,3,2,5,0,3,5,1,4,6,2,4};
	int y = year - (month < 2);
	y=(y + y/4 - y/100 + y/400 + t[month]+1) % 7;
        // y = 0 for Sunday, 1 for Monday.. etc.
	return y;
}


The problem is, i have never understood the arithmetic in my function and in yours likewise.
Can you please explain the arithmetic??
My mistake, i left out that the 1st day of January 1901 is a Tuesday
@azl4182

My mistake, i left out that the 1st day of January 1901 is a Tuesday


No, you mentioned that in your original post.

@shadowCODE

The int t[] array is for how many years to add to the year. (y)
My mistake. It should read..
The int t[] array is for how many years to subtract from the year. (y)
y will equal the year minus the month sent IF it's less than 2. (0 or 1)
Add 1 to year if it's leap year, plus the value of t[month] plus 1, then get the modulus 7 of it.
That value (y), will be the first day of the given month.

That's the way I understand it. If I'm mistaken, sorry. All I know, is it works.
Last edited on
That's the way I understand it. If I'm mistaken, sorry. All I know, is it works

LIKE THIS.
*update*
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
#include <iostream>
using namespace std;

int main()
{
    int month, year, days;
    char ch;

	cout << "Enter a month and year between 1/1901 and 12/2099: ";
    cin >> month >> ch >> year;
    	if (year < 1901 || year > 2099) cout << "Sorry, the date entered is invalid." << endl; 
    	
		else		
    	while (true)
			{
				switch (month)
				{
					case 1: "January"; days = 31; break;
					case 2: "February"; days = 31;
						if (year % 4 != 0) days = 28;
							else days = 29; break;
					case 3: "March"; days = 31; break;
					case 4: "April"; days = 30; break;
					case 5: "May"; days = 31; break;
					case 6: "June"; days = 30; break;
					case 7: "July"; days = 31; break;
					case 8: "August"; days = 31; break;
					case 9: "September"; days = 30; break;
					case 10: "October"; days = 31; break;
					case 11: "November"; days = 30; break;
					case 12: "December "; days = 31; break;
					default: cout << "Sorry, the month entered is invalid." << endl;
					
					cout << "Calendar: " << month << ", " << year; break;
					cout << "Sun Mon Tue Wed Thu Fri"; break;
						if (month < 12 && month > 1)
							{
								int numDays, firstDay, days, day;
								firstDay = (2 + numDays) % 7; firstDay;
	
   						   		 for (day = 1; day <= days; day++)
								{
		
    								if ((day + fisrtday) % 7 == 0)
    								cout << endl;
								}   
				
							}	
				}
				
			}
		
			while (true)
				
			{
				char cc;
				
				cout << "Do you want to print another calendar? (Y/N): ";
				cin >> cc;
				if (cc != 'y' && cc != 'Y') break;
			}

    return 0;
}


It is still incomplete, I'm having trouble calculating the number of days. Any advice?
Topic archived. No new replies allowed.