I need help in order to get the below part right Required! You MUST have a singl
ID: 3547242 • Letter: I
Question
I need help in order to get the below part right
Required! You MUST have a single function named "printMonth" that prints out ANY month, based on its parameters. In other words, don't create 12 separate functions, one named "printJanuary", one named "printFebruary," and so on. In addition, you must have only ONE call to this printMonth function, in a loop that calls the function 12 times.
Can somehelp me with setting it up with a switch statement or something that fits the above criteria?
#include <iostream>
#include <iomanip>
using namespace std;
bool isLeapYear (int Year);
void printMonth (int dayOfWeek, int daysInMonth);
int main () {
int currentYear;
int firstDay;
int totalDays;
cout << "What year do you wand a calendar for? ";
cin >> currentYear;
cout << "What day of the week does January 1 fall on (0 for Sunday, 1 for Monday, etc.)? ";
cin >> firstDay;
cout << endl << setw(17) << currentYear << endl << endl;
cout << setw(18) << "January" << endl;
totalDays = 31;
printMonth (firstDay, totalDays);
cout << setw(18) << "February" << endl;
firstDay = (firstDay + totalDays) % 7;
if (isLeapYear (currentYear))
totalDays = 29;
else
totalDays = 28;
printMonth (firstDay, totalDays);
cout << setw(18) << "March" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
printMonth (firstDay, totalDays);
cout << setw(18) << "March" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
printMonth (firstDay, totalDays);
cout << setw(18) << "April" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 30;
printMonth (firstDay, totalDays);
cout << setw(18) << "May" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
printMonth (firstDay, totalDays);
cout << setw(18) << "June" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 30;
printMonth (firstDay, totalDays);
cout << setw(18) << "July" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
printMonth (firstDay, totalDays);
cout << setw(18) << "August" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
printMonth (firstDay, totalDays);
cout << setw(18) << "September" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
printMonth (firstDay, totalDays);
cout << setw(18) << "October" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
printMonth (firstDay, totalDays);
cout << setw(18) << "November" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 30;
printMonth (firstDay, totalDays);
cout << setw(18) << "December" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
printMonth (firstDay, totalDays);
}
bool isLeapYear (int year) {
if ((year % 400 == 0) ||
(year % 4 == 0 && year % 100 != 0))
return true;
else
return false;
}
void printMonth (int dayOfWeek, int daysInMonth) {
cout << setw(4) << "S"
<< setw(4) << "M"
<< setw(4) << "T"
<< setw(4) << "W"
<< setw(4) << "T"
<< setw(4) << "F"
<< setw(4) << "S" << endl;
cout << "------------------------------" << endl;
int date = 1;
int count;
for (count = 0; count < dayOfWeek; count++) {
cout << setw(4) << " ";
}
for (count = 0; count < 7 - dayOfWeek; count++) {
cout << setw(4) << date;
date++;
}
for (count = 0; date <= daysInMonth; count++) {
cout << setw(4) << date;
date++;
if (count % 7 == 0)
cout << endl;
}
cout << endl << endl;
}
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
bool isLeapYear (int Year);
void printMonth (int dayOfWeek, int daysInMonth);
int main ()
{
int currentYear;
int firstDay;
int totalDays;
cout << "What year do you wand a calendar for? ";
cin >> currentYear;
cout << "What day of the week does January 1 fall on (0 for Sunday, 1 for Monday, etc.)? ";
cin >> firstDay;
cout << endl << setw(17) << currentYear << endl << endl;
for(int i=0; i<12; i++)
{
switch(i)
{
case 0:
{
cout << setw(18) << "January" << endl;
totalDays = 31;
}
break;
case 1:
{
cout << setw(18) << "February" << endl;
firstDay = (firstDay + totalDays) % 7;
if (isLeapYear (currentYear))
totalDays = 29;
else
totalDays = 28;
}
break;
case 2:
{
cout << setw(18) << "March" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
}
break;
case 3:
{
cout << setw(18) << "March" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
}
break;
case 4:
{
cout << setw(18) << "April" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 30;
}
break;
case 5:
{
cout << setw(18) << "May" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
}
break;
case 6:
{
cout << setw(18) << "June" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 30;
}
break;
case 7:
{
cout << setw(18) << "July" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
}
break;
case 8:
{
cout << setw(18) << "August" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
}
break;
case 9:
{
cout << setw(18) << "September" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
} break;
case 10:
{
cout << setw(18) << "October" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
}
break;
case 11:
{
cout << setw(18) << "November" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 30;
}
break;
case 12:
{
cout << setw(18) << "December" << endl;
firstDay = (firstDay + totalDays) % 7;
totalDays = 31;
}
break;
}
printMonth (firstDay, totalDays);
}
return 0;
}
bool isLeapYear (int year) {
if ((year % 400 == 0) ||
(year % 4 == 0 && year % 100 != 0))
return true;
else
return false;
}
void printMonth (int dayOfWeek, int daysInMonth) {
cout << setw(4) << "S"
<< setw(4) << "M"
<< setw(4) << "T"
<< setw(4) << "W"
<< setw(4) << "T"
<< setw(4) << "F"
<< setw(4) << "S" << endl;
cout << "------------------------------" << endl;
int date = 1;
int count;
for (count = 0; count < dayOfWeek; count++) {
cout << setw(4) << " ";
}
for (count = 0; count < 7 - dayOfWeek; count++) {
cout << setw(4) << date;
date++;
}
for (count = 0; date <= daysInMonth; count++) {
cout << setw(4) << date;
date++;
if (count % 7 == 0)
cout << endl;
}
cout << endl << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.