Challeneg (1)

CS1440- Spring 2002

Due: When you need the next one


Write a C++ program that prints a calendar for a given year. The program prompts the user for three inputs;

1) The year for which you are generating the calendar.

2) The day of the week that January first is on, you will use the following notation to set the day of the week:

0 Sunday 1 Monday 2 Tuesday 3 Wednesday

4 Thursday 5 Friday 6 Saturday


3) The output file name.


Your program should generate the calendar similar to the one shown in the example output below. The calendar should be printed into the output file. Your program should be able to handle leap years. A leap year is a year in which we have 366 days. That extra day comes at the end of February. Thus, a leap year have 366 days with 29 days in February. A century year is a leap year if it is devisable by 400. Other years devisable by 4 but not by 100 are also leap years. Example: year 2004 is a leap year but year 2100 is not a leap year. 2000 is devisable by 400, 2100 is devisable by 100.


You can use as many functions as you wish. Your code has to clearly describe its functionality and the instructions on how to run it.


Sample Input:


Please enter the year for which you wish to generate the calendar: 2004

Please enter the day of the week that January first is on: 4


Please note that day 4 corresponds to Thursday.


Sample output:


Calendar for year 2004



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




Sun Mon Tue Wed Thu Fri Sat

1 2 3 4 5 6 7

.. .. .. .. .. ..

.. ..




Your output must be in the same format. You can use the function setw to fix the width of the printing field. This allows you to take up a fixed number of spaces for your numbers even if your do not need them. Please see Display 5.4 for the list of flags that you can use with setref. There are some examples after that display that should be very helpful.

Hint: Left align all numbers and use setw(n), n is the number of spaces you wish to use for your numbers. In the above example I have used 11 spaces, i.e. n =11.


Example on fixing the filed width:

cout << setw(4) << 10 << setw(5) << 12;


This prints number 10 with 4 filed space, of course, 3 of them are not used but we could print up to 1000. The next number is displayed with 5 field spaces.