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.


Expert's answer

#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;
}
LATEST TUTORIALS
APPROVED BY CLIENTS