Write a C++ program that prints a calendar for a given year. You can use the pro
ID: 3660754 • Letter: W
Question
Write a C++ program that prints a calendar for a given year. You can use the program ex74.cpp as the starting point. 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 a 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 has 366 days with 29 days in February. A century year is a leap year if it is divisible by 400. Other years divisible by 4 but not by 100 are also leap years.
Example: Year 2000 is a leap year because it is divisble by 400. Year 2004 is a leap year because it is divisible by 4 but not by 100.
You can use as many functions as you wish. Your program should clearly describe the functionality of each function and should display the instructions on how to run the program.
Sample Input:
Enter the name of the output file: cal2004
Enter the year for which you wish to generate the calendar: 2004
Enter the day of the week that January first is on: 4
Sample output:
Calendar for year 2004
January
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
February
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
Explanation / Answer
int getYear(); // has the user enter a valid year
bool isLeap(int year); // check for leap years
void dayName(); // prints the names for the day of the week
void monthNameHeader(int year); // puts head for the month name
int startDay(int year); // decides what week day Jan starts on
int monthCount(int counter); // how many days are in each month
void newMonth(int startDOW); // what day of the week new month starts on
void printAll(int year); // puts everything together, prints to screen
int year = 0; // uesr inputed year || rand gen per 0
int counter = 1; // counter for month name & # days in month
int startDOW, // day of the week Jan starts on
wrap, // check for if weekday is Saturday
daysInMonth; // total days in each month
int weekNumber = 0; // flag for first week of the month
int main()
{
year = getYear(); // has user enter year number
printAll(year);
return 0;
}
int getYear() //prompts the user to enter a valid year
{
char c;
srand(time(NULL));
cout << "Enter the year, or 0 for and random year: ";
do { // gets whole number value
cin.get(c);
if(isdigit(c))
{
year=year*10;
year +=(int)(c-'0');
}
} while(c!=' ');
if (year == 0) // if no response or 0 are enter, random a year
{
year = rand() % 8600 + 1400;
cout << " The random year " << year << " will be evaluated ";
}
return year;
}
bool isLeap(int year) // checking for possible leap year
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false; // else return false
}
void dayName()
{
cout << " S M T W T F S" << endl;
cout << "---------------------" << endl;
}
void monthNameHeader(int year)
{
switch (counter)
{
case 1:
cout << " January " << year << endl;
break;
case 2:
cout << " February " << year << endl;
break;
case 3:
cout << " March" << year << endl;
break;
case 4:
cout << " April " << year << endl;
break;
case 5:
cout << " May " << year << endl;
break;
case 6:
cout << " June " << year << endl;
break;
case 7:
cout << " July " << year << endl;
break;
case 8:
cout << " August " << year << endl;
break;
case 9:
cout << " September " << year << endl;
break;
case 10:
cout << " October " << year << endl;
break;
case 11:
cout << " November " << year << endl;
break;
case 12:
cout << " December " << year << endl;
break;
}
}
int monthCount(int counter) // how many days are in the month
{
switch (counter)
{
case 1:
daysInMonth = 31; // current month days
break;
case 2: // checks for possible leap year
if(isLeap(year))
daysInMonth = 29;
if(!isLeap(year))
daysInMonth = 28;
break;
case 3:
daysInMonth = 31;
break;
case 4:
daysInMonth = 30;
break;
case 5:
daysInMonth = 31;
break;
case 6:
daysInMonth = 30;
break;
case 7:
daysInMonth = 31;
break;
case 8:
daysInMonth = 31;
break;
case 9:
daysInMonth = 30;
break;
case 10:
daysInMonth = 31;
break;
case 11:
daysInMonth = 30;
break;
case 12:
daysInMonth = 31;
break;
}
}
int startDay(int year)
{
startDOW = (year + (year - 1 ) /4 - (year - 1) / 100 + (year - 1) /400) %7;
return startDOW; // formula for what DoWeek year starts on
}
void printAll(int year)
{
for (counter = 1; counter <= 12; counter++)
{
monthNameHeader(year); // prints month day
dayName(); // prints the name of days
if (counter==1)
wrap = startDay(year) ; // what day Jan starts on
else
startDOW = wrap; // what day other months start on
cout << " ";
for (int loopCount = 0; loopCount < startDOW; loopCount++)
{
cout << " "; // how many space to indent new month
}
monthCount(counter); // how many days in month
for (int dayCounter=1;dayCounter<=daysInMonth; dayCounter++)
{
if (wrap == 7) //if Saturday, carriage return
{
cout << " ";
wrap = 0; //resets day of week counter
weekNumber++; //no longer first week of month
}
if (dayCounter<10) //adds space for single digit days
cout << " ";
cout << dayCounter << " "; //prints the day #
wrap++;
}
// cout << " this month starts on day number " << startDOW; *testing*
// cout << " days in this month are " << daysInMonth; *testing*
cout << " ";
system("PAUSE");
cout << endl;
} // end BIG for loop
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.