Help me answer these C++ questions with my code below? #include<iostream> #inclu
ID: 3886149 • Letter: H
Question
Help me answer these C++ questions with my code below?
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
//Class Month definition
class Month
{
//Data member to store month number
int monthNumber;
public:
//Member functions
void set_month(int);
int get_month();
void input();
void output();
void advance();
void before();
void after();
void same();
};//End of class
//Function to check month is same or not
void Month::same()
{
//To store month number
int mo;
//Variable month - to store month in character form.
//Variable monthPart - to store month first three letters
string month, monthPart;
//Accept the month
cout<<" Enter either Month Number / Month Name / First three letter of month: ";
cin>>month;
//Checks whether the first character is number or not
if(isdigit(month[0]))
{
//Checks whether double digit
if(month.length()== 2)
{
//Converts to integer
mo = month[0] - '0';
//Multiply by 10 and adds the next digit to make it double digit
mo = mo * 10;
int next = month[1] - '0';
mo = mo + next;
}//End of inner if
//If single digit
else
//Converts to integer
mo = month[0] - '0';
//Checks for invalid entry
if(mo < 0 && mo > 12)
cout<<" Invalid month number. ";
}//End of outer if
//If in character form
else
{
//Subtracts the first three characters from the entry
monthPart = month.substr(0,3);
//Converts to upper case
for(int x = 0; x < 3; x++)
monthPart[x] = toupper(monthPart[x]);
//Compares the month and stores its corresponding number form
if(monthPart.compare("JAN") == 0)
mo = 1;
else if(monthPart.compare("FEB") == 0)
mo = 2;
else if(monthPart.compare("MAR") == 0)
mo = 3;
else if(monthPart.compare("APR") == 0)
mo = 4;
else if(monthPart.compare("MAY") == 0)
mo = 5;
else if(monthPart.compare("JUN") == 0)
mo = 6;
else if(monthPart.compare("JUL") == 0)
mo = 7;
else if(monthPart.compare("AUG") == 0)
mo = 8;
else if(monthPart.compare("SEP") == 0)
mo = 9;
else if(monthPart.compare("OCT") == 0)
mo = 10;
else if(monthPart.compare("NOV") == 0)
mo = 11;
else if(monthPart.compare("DEC") == 0)
mo = 12;
else
cout<<" Invalid month name. ";
}//End of else
//Checks for similarity
if(mo == monthNumber)
cout<<" Same month ";
else
cout<<" Not Same month ";
}//End of function
void Month::input()
{
//Variable month - to store month in character form.
//Variable monthPart - to store month first three letters
string month, monthPart;
//To store month number
int mo;
//Accept the month
cout<<" Enter either Month Number / Month Name / First three letter of month: ";
cin>>month;
//Checks whether the first character is number or not
if(isdigit(month[0]))
{
//Checks whether double digit
if(month.length()== 2)
{
//Converts to integer
mo = month[0] - '0';
//Multiply by 10 and adds the next digit to make it double digit
mo = mo * 10;
int next = month[1] - '0';
mo = mo + next;
}//End of inner if
//If single digit
else
//Converts to integer
mo = month[0] - '0';
//Checks for invalid entry
if(mo < 0 && mo > 12)
cout<<" Invalid month number. ";
//Set the month number
set_month(mo);
}//End of outer if
//If in character form
else
{
//Subtracts the first three characters from the entry
monthPart = month.substr(0,3);
//Converts to upper case
for(int x = 0; x < 3; x++)
monthPart[x] = toupper(monthPart[x]);
//Compares the month and stores its corresponding number form
if(monthPart.compare("JAN") == 0)
set_month(1);
else if(monthPart.compare("FEB") == 0)
set_month(2);
else if(monthPart.compare("MAR") == 0)
set_month(3);
else if(monthPart.compare("APR") == 0)
set_month(4);
else if(monthPart.compare("MAY") == 0)
set_month(5);
else if(monthPart.compare("JUN") == 0)
set_month(6);
else if(monthPart.compare("JUL") == 0)
set_month(7);
else if(monthPart.compare("AUG") == 0)
set_month(8);
else if(monthPart.compare("SEP") == 0)
set_month(9);
else if(monthPart.compare("OCT") == 0)
set_month(10);
else if(monthPart.compare("NOV") == 0)
set_month(11);
else if(monthPart.compare("DEC") == 0)
set_month(12);
else
cout<<" Invalid month name. ";
}//End of outer else
}//End of function
//Function to display the next month
void Month::after()
{
//Adds one to month number
monthNumber++;
//Checks if it exceeds 12 then set it to one for January
if(monthNumber > 12)
{
monthNumber = 1;
}//End of if
//Display the month name
output();
}//End of function
//Function to display the previous month
void Month::before()
{
//Subtracts one to month number
monthNumber--;
//Checks if it is zero then set it to 12 for December
if(monthNumber == 0)
{
monthNumber = 12;
}//End of if
//Displays the output
output();
}//End of function
//Function to advance the month by inputed number
void Month::advance()
{
int no, diff;
//Accept the advance number
cout<<" Enter the advance value: ";
cin>>no;
//Adds the advance month number to month number
monthNumber = no + monthNumber;
//If it exceeds 12 then subtract it from 12 and store it in month number
if(monthNumber > 12)
{
monthNumber = monthNumber - 12;
}//End of if
//Displays the output
output();
}//End of function
//Function to display the month name
void Month::output()
{
//Month number is passed in switch to match the case
switch(monthNumber)
{
case 1:
cout<<"January";
break;
case 2:
cout<<"February";
break;
case 3:
cout<<"March";
break;
case 4:
cout<<"April";
break;
case 5:
cout<<"May";
break;
case 6:
cout<<"June";
break;
case 7:
cout<<"July";
break;
case 8:
cout<<"August";
break;
case 9:
cout<<"September";
break;
case 10:
cout<<"October";
break;
case 11:
cout<<"November";
break;
case 12:
cout<<"December";
break;
}//End of switch
}//End of function
//Function to return the month number
int Month::get_month()
{
return monthNumber;
}//End of function
//Function to set the month number
void Month::set_month(int no)
{
monthNumber = no;
}//End of function
//Function to return the user choice
int menu()
{
//To store user choice
int ch;
//Displays menu
cout<<" 1 Advance ";
cout<<" 2 Before ";
cout<<" 3 After ";
cout<<" 4 Same ";
cout<<" 5 Exit ";
//Accepts user choice
cout<<" Enter your choice: ";
cin>>ch;
//Returns user choice
return ch;
}//End of function
//Main function definition
int main()
{
//Month class object created
Month m;
//Accepts the user month number or name
m.input();
//To store user choice returned by the menu function
int ch;
//Loops till user choice is 5
do
{
//Accept user choice
ch = menu();
//Calls user function based on user choice
switch(ch)
{
case 1:
m.advance();
break;
case 2:
m.before();
break;
case 3:
m.after();
break;
case 4:
m.same();
break;
case 5:
exit(0);
default:
cout<<" Invalid choice. ";
}//End of switch
}while(1);// End of while
return 0;
}//End of main function
Explanation / Answer
The code is fixed
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
//Class Month definition
class Month
{
//Data member to store month number
int monthNumber;
public:
//Member functions
void set_month(int);
int get_month();
void input();
void output();
void advance();
void before();
void after();
void same();
};//End of class
//Function to check month is same or not
void Month::same()
{
//To store month number
int mo;
//Variable month - to store month in character form.
//Variable monthPart - to store month first three letters
string month, monthPart;
//Accept the month
cout<<" Enter either Month Number / Month Name / First three letter of month: ";
cin>>month;
//Checks whether the first character is number or not
if(isdigit(month[0]))
{
//Checks whether double digit
if(month.length()== 2)
{
//Converts to integer
mo = month[0] - '0';
//Multiply by 10 and adds the next digit to make it double digit
mo = mo * 10;
int next = month[1] - '0';
mo = mo + next;
}//End of inner if
//If single digit
else
//Converts to integer
mo = month[0] - '0';
//Checks for invalid entry
if(mo < 0 && mo > 12)
cout<<" Invalid month number. ";
}//End of outer if
//If in character form
else
{
//Subtracts the first three characters from the entry
monthPart = month.substr(0,3);
//Converts to upper case
for(int x = 0; x < 3; x++)
monthPart[x] = toupper(monthPart[x]);
//Compares the month and stores its corresponding number form
if(monthPart.compare("JAN") == 0)
mo = 1;
else if(monthPart.compare("FEB") == 0)
mo = 2;
else if(monthPart.compare("MAR") == 0)
mo = 3;
else if(monthPart.compare("APR") == 0)
mo = 4;
else if(monthPart.compare("MAY") == 0)
mo = 5;
else if(monthPart.compare("JUN") == 0)
mo = 6;
else if(monthPart.compare("JUL") == 0)
mo = 7;
else if(monthPart.compare("AUG") == 0)
mo = 8;
else if(monthPart.compare("SEP") == 0)
mo = 9;
else if(monthPart.compare("OCT") == 0)
mo = 10;
else if(monthPart.compare("NOV") == 0)
mo = 11;
else if(monthPart.compare("DEC") == 0)
mo = 12;
else
cout<<" Invalid month name. ";
}//End of else
//Checks for similarity
if(mo == monthNumber)
cout<<" Same month ";
else
cout<<" Not Same month ";
}//End of function
void Month::input()
{
//Variable month - to store month in character form.
//Variable monthPart - to store month first three letters
string month, monthPart;
//To store month number
int mo;
//Accept the month
cout<<" Enter either Month Number / Month Name / First three letter of month: ";
cin>>month;
//Checks whether the first character is number or not
if(isdigit(month[0]))
{
//Checks whether double digit
if(month.length()== 2)
{
//Converts to integer
mo = month[0] - '0';
//Multiply by 10 and adds the next digit to make it double digit
mo = mo * 10;
int next = month[1] - '0';
mo = mo + next;
}//End of inner if
//If single digit
else
//Converts to integer
mo = month[0] - '0';
//Checks for invalid entry
if(mo < 0 && mo > 12)
cout<<" Invalid month number. ";
//Set the month number
set_month(mo);
}//End of outer if
//If in character form
else
{
//Subtracts the first three characters from the entry
monthPart = month.substr(0,3);
//Converts to upper case
for(int x = 0; x < 3; x++)
monthPart[x] = toupper(monthPart[x]);
//Compares the month and stores its corresponding number form
if(monthPart.compare("JAN") == 0)
set_month(1);
else if(monthPart.compare("FEB") == 0)
set_month(2);
else if(monthPart.compare("MAR") == 0)
set_month(3);
else if(monthPart.compare("APR") == 0)
set_month(4);
else if(monthPart.compare("MAY") == 0)
set_month(5);
else if(monthPart.compare("JUN") == 0)
set_month(6);
else if(monthPart.compare("JUL") == 0)
set_month(7);
else if(monthPart.compare("AUG") == 0)
set_month(8);
else if(monthPart.compare("SEP") == 0)
set_month(9);
else if(monthPart.compare("OCT") == 0)
set_month(10);
else if(monthPart.compare("NOV") == 0)
set_month(11);
else if(monthPart.compare("DEC") == 0)
set_month(12);
else
cout<<" Invalid month name. ";
}//End of outer else
}//End of function
//Function to display the next month
void Month::after()
{
//Adds one to month number
monthNumber++;
//Checks if it exceeds 12 then set it to one for January
if(monthNumber > 12)
{
monthNumber = 1;
}//End of if
//Display the month name
output();
}//End of function
//Function to display the previous month
void Month::before()
{
//Subtracts one to month number
monthNumber--;
//Checks if it is zero then set it to 12 for December
if(monthNumber == 0)
{
monthNumber = 12;
}//End of if
//Displays the output
output();
}//End of function
//Function to advance the month by inputed number
void Month::advance()
{
int no, diff;
//Accept the advance number
cout<<" Enter the advance value: ";
cin>>no;
//Adds the advance month number to month number
monthNumber = no + monthNumber;
//If it exceeds 12 then subtract it from 12 and store it in month number
if(monthNumber > 12)
{
monthNumber = monthNumber - 12;
}//End of if
//Displays the output
output();
}//End of function
//Function to display the month name
void Month::output()
{
//Month number is passed in switch to match the case
switch(monthNumber)
{
case 1:
cout<<"January";
break;
case 2:
cout<<"February";
break;
case 3:
cout<<"March";
break;
case 4:
cout<<"April";
break;
case 5:
cout<<"May";
break;
case 6:
cout<<"June";
break;
case 7:
cout<<"July";
break;
case 8:
cout<<"August";
break;
case 9:
cout<<"September";
break;
case 10:
cout<<"October";
break;
case 11:
cout<<"November";
break;
case 12:
cout<<"December";
break;
}//End of switch
}//End of function
//Function to return the month number
int Month::get_month()
{
return monthNumber;
}//End of function
//Function to set the month number
void Month::set_month(int no)
{
monthNumber = no;
}//End of function
//Function to return the user choice
int menu()
{
//To store user choice
int ch;
//Displays menu
cout<<" 1 Advance ";
cout<<" 2 Before ";
cout<<" 3 After ";
cout<<" 4 Same ";
cout<<" 5 Exit ";
//Accepts user choice
cout<<" Enter your choice: ";
cin>>ch;
//Returns user choice
return ch;
}//End of function
//Main function definition
int main()
{
//Month class object created
Month m;
//Accepts the user month number or name
m.input();
//To store user choice returned by the menu function
int ch;
//Loops till user choice is 5
do
{
//Accept user choice
ch = menu();
//Calls user function based on user choice
switch(ch)
{
case 1:
m.advance();
break;
case 2:
m.before();
break;
case 3:
m.after();
break;
case 4:
m.same();
break;
case 5:
exit(0);
default:
cout<<" Invalid choice. ";
}//End of switch
}while(1);// End of while
return 0;
}//End of main function
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.