Question
THIS IS C++ PROGRAMMING: --I need some assistance on implementing functions (They will be italized) int main() { char choice; int day, month, year; do
THIS IS C++ PROGRAMMING:
--I need some assistance on implementing functions (They will be italized)
int main()
{
char choice;
int day, month, year;
do
{
system("CLS");
menu();// display menu by calling menu function
cout << " \tPlease enter your choice: ";// ask user to enter his/her choice on menu
cin >> choice;// get user's choice
switch (choice)
{
case '1': // will query a date.
get_input(month, day, year);
print_calendar(month, day, year);// print out the day of week of specified date
break;
case '2': // print out the calendar of a given month.
get_input(month, year);
print_calendar(month, year);//print out the calendar of specified month
break;
case '3': // print out the calendar of a given year.
get_input(year);
print_calendar(year);// print out the calendar of specified year
break;
case '4': // print out the calendar of specified year to yourname.txt file
// you implement this ******************
break;
case '5': // quit the program.
cout << " \tThank you for using calendar program! "; // you may replace your name here
break;
default: // other choices that are not the options on menu.
cout << "Error, no such choice. Please choose from Menu. ";//print out error message
break;
}
cout << " \t";
system("pause");
} while (choice != '5');
return 0;
}
// you implement all function stubs here
void menu()
{
cout << " \t***************************************************";
cout << " \t* Menu *";
cout << " \t* 1. Query a date *";
cout << " \t* 2. Print calendar of a given month *";
cout << " \t* 3. Print calendar of a given year *";
// add one more options here. Notice that the quit option becomes option 5 now
cout << " \t* 5. Quit *";
cout << " \t***************************************************";
}
void get_input(int& month, int& day, int& year)
{
cout << " \tEnter the date in form of mm dd yyyy: ";
cin >> month >> day >> year;
while (!isValid(month, day, year)) // as long as the date is NOT valid, keep asking to reenter the date
{
cout << " \tThe date you entered is NOT valid. Please reenter a valid date: ";
cin >> month >> day >> year;
}
}
void get_input(int& month, int& year)
{
cout << " \tEnter the month and year in form of mm yyyy: ";
cin >> month >> year;
while (!isValid(month, year)) // as long as the month is NOT valid, keep asking to reenter the month
{
cout << " \tThe month you entered is NOT valid. Please reenter a valid month: ";
cin >> month >> year;
}
}
void get_input(int& year)
{
cout << " \tEnter the year as yyyy: ";
cin >> year;
while (!isValid(year)) // as long as the year is NOT valid, keep asking to reenter the year
{
cout << " \tThe year you entered is NOT valid. Please reenter a valid year: ";
cin >> year;
}
}
bool isValid(int month, int day, int year)
{
bool result = true; // temporary assume that the result is true
switch (month) // update result based on day
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
result = day >= 1 && day <= 31;
break;
case 4: case 6: case 9: case 11:
result = day >= 1 && day <= 30;
break;
case 2:
if (isLeapYear(year))
result = day >= 1 && day <= 29;
else
result = day >= 1 && day <= 28;
break;
}
return result && isValid(month, year); // day is valid and (month and year are valid)
}
bool isValid(int month, int year)
{
return month >= 1 && month <= 12 && year > 1582; // month is between 1 and 12 and year is later than 1582
}
bool isValid(int year)
{
return year > 1582; // year is later than 1582
}
bool isLeapYear(int year)
{
return ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0));
}
int getCenturyValue(int year)
{
return 2 * (3 - (year / 100) % 4);
}
int getYearValue(int year)
{
return year % 100 + year % 100 / 4;
}
int getMonthValue(int month, int year)
{
int result;
switch (month)
{
case 1:
if (isLeapYear(year))
result = 6;
else result = 0;
break;
case 2:
if (isLeapYear(year))
result = 2;
else
result = 3;
break;
case 3: case 11:
result = 3;
break;
case 4: case 7:
result = 6;
break;
case 5:
result = 1;
break;
case 6:
result = 4;
break;
case 8:
result = 2;
break;
case 9:case 12:
result = 5;
break;
default: // case 10
result = 0;
break;
}
return result;
}
int dayOfWeek(int month, int day, int year)
{
if (!isValid(month, day, year)) return -1;
return (getCenturyValue(year) + getMonthValue(month, year) + getYearValue(year) + day) % 7;
}
void print_calendar(int month, int day, int year)
{
if (!isValid(month, day, year)) { // if the date is NOT valid, print out error message and return
cout << " \tThe date is NOT valid!";
return;
}
cout << " \t" << monthName(month) << " " << day << " , " << year << " is " << dayOfWeek(dayOfWeek(month, day, year));
}
void print_calendar(int month, int year)
{
if (!isValid(month, year)) { // if the month is NOT valid, print out error message and return
cout << " \tThe month is NOT valid!";
return;
}
// print out the month and year
cout << " \t " << monthName(month) << ", " << year << endl;
// print out day names
cout << " \t SUN MON TUE WED THU FRI SAT \t";
int spaceCount = dayOfWeek(month, 1, year);
// print out spaces
for (int i = 0; i < spaceCount; i++)
cout << setw(4) << "";
// calculate the days in the month
int days = daysInMonth(month, year);
// print out all days
for (int j = 1; j <= days; j++)
{
cout << setw(4) << j;
if ((spaceCount + j) % 7 == 0) // go to new line for a new week
cout << " \t";
}
}
void print_calendar(int year)
{
if (!isValid(year)) { // if the year is NOT valid, print out error message and return
cout << " \tThe year is NOT valid!";
return;
}
for (int i = 1; i <= 12; i++) // print out 12 month calendar
{
print_calendar(i, year); // print out ith month
cout << endl;
}
}
string monthName(int month)
{
string result;
switch (month)
{
case 1:
result = "January";
break;
case 2:
result = "February";
break;
case 3:
result = "March";
break;
case 4:
result = "April";
break;
case 5:
result = "May";
break;
case 6:
result = "June";
break;
case 7:
result = "July";
break;
case 8:
result = "August";
break;
case 9:
result = "September";
break;
case 10:
result = "October";
break;
case 11:
result = "November";
break;
case 12:
result = "December";
break;
default:
result = "Not valid month";
break;
}
return result;
}
string dayOfWeek(int day)
{
string result;
switch (day)
{
case 0:
result = "Sunday";
break;
case 1:
result = "Monday";
break;
case 2:
result = "Tuesday";
break;
case 3:
result = "Wednesday";
break;
case 4:
result = "Thursday";
break;
case 5:
result = "Friday";
break;
case 6:
result = "Saturday";
break;
default:
result = "Invalid date";
break;
}
return result;
}
int daysInMonth(int month, int year)
{
int result;
switch (month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
result = 31;
break;
case 4: case 6: case 9: case 11:
result = 30;
break;
case 2:
if (isLeapYear(year))
result = 29;
else result = 28;
break;
default:
result = -1;
break;
}
return result;
}
bool print_calendar(int year, string fileName)
{
// if year is not valid, print out error message and return false
// declare a local ofstream variable and open file to write
// if file open failed, print out error message and return false
// use for loop to print out 12 months to the file
// close file
// return true
}
void print_calendar(int month, int year, ofstream& output)
{
// if the month is NOT valid, print out error message and return
// print out the month and year
// print out day names
// calculate spaces before the first day of the month
// print out spaces
// calculate the days in the month
// print out all days, using a for loop
// two more new lines
// Note this function is very similar to the one print to screen
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started