Answer to Question #273286 in C++ for devi

Question #273286

2. Accept a month as number (eg: 3 is March), and print month name. Any invalid month should throw an exception, “Month Error”. The program should continuously ask the user to enter the month until the user choses to exit.


1
Expert's answer
2021-11-29T18:30:32-0500
#include<iostream>
using namespace std;
int main(){
	int x;
	do{
		string month[12]={"January", "February","March","April", "May", "June","July","August","September","October","November","December"};
		cout<<"Enter month in number:\n";
		int m ;
		cin>>m;
		switch(m){
			case 0 ... 11:
				cout<<"Month is  "<<month[m-1]<<endl;
				break;
			default:
				cout<<"Month Error\n";
				break;
		}
		cout<<"Enter 0 to exit or 1 to continue\n";
		cin>>x;
		
	}while(x!=0);
	
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS