Generate a program which takes a number between 1-12 as a user input and prints
ID: 1814476 • Letter: G
Question
Generate a program which takes a number between 1-12 as a user input and prints the associated month.
1. Prompt the user to input the number of the month
2. Using a switch statement, print the name of the associated month.
a. For Example, user enters 4, the program shall print April
3. If the user does not enter a valid number, print an error to console
4. Prompt the user if they wish to repeat
a. if yes, repeat the program using a do-while loop
b. be able to accept both a lowercase and uppercase y as the repeat response
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int n_month;
char repeat = 'Y';
do
{
cout<<"Enter the number of month:";
cin>>n_month;
switch(n_month)
{
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;
default :
cout<<"Error! Invalid month";
break;
}
cout<<" Enter 'Y' to repeat the program:";
cin>>repeat;
}while(toupper(repeat)=='Y');
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.