Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Goal: Use the switch case statement and perform input validation. Assignment: You are programming a simple utility that identifies the meteorological season based on the
Goal: Use the switch case statement and perform input validation.
Assignment: You are programming a simple utility that identifies the meteorological season based on the calendar month and hemisphere entered by the user. The program asks for the hemisphere N for North, S for South asks for the month as a number for January, for February, etc. and then display the corresponding meteorological season.
Note: From now on NH stands for Northern Hemisphere, and SH stands for Southern Hemisphere.
For this program, assume:
Winter NH Summer SH: DecFeb
Spring NH Fall SH: MarMay
Summer NH Winter SH: Jun Aug
Fall NH Sprint SH: SepNov
If the user enters an invalid hemisphere character, display the message "Please enter a valid hemisphere N S If the user enters a number less than or greater than for the month, display the message "Please enter a valid month number
Sample Runs: User input is in between square brackets.
Enter a hemisphere N S: N
Enter a month number :
The season is Spring.
Enter a hemisphere N S: n
Please enter a valid hemisphere N S
Enter a hemisphere N S: S
Enter a month number :
Please enter a valid month number
Note: For this exercise, you will need to write a complete C program that includes a main function, any needed libraries, etc.
#include
#include For toupper
using namespace std;
int main
char hemisphere;
int month;
string season;
Input for hemisphere
cout "Enter a hemisphere N S: ;
cin hemisphere;
hemisphere toupperhemisphere; Convert to uppercase to handle lowercase input
Input validation for hemisphere
if hemisphere N && hemisphere S
cout "Please enter a valid hemisphere N S endl;
return ; Exit the program with an error code
Input for month
cout "Enter a month number : ;
cin month;
Input validation for month
if month month
cout "Please enter a valid month number endl;
return ; Exit the program with an error code
Determine the season based on hemisphere and month
switch hemisphere
case N: Northern Hemisphere
switch month
case :
case :
case :
season "Winter";
break;
case :
case :
case :
season "Spring";
break;
case :
case :
case :
season "Summer";
break;
case :
case :
case :
season "Fall";
break;
break;
case S: Southern Hemisphere
switch month
case :
case :
case :
season "Summer";
break;
case :
case :
case :
season "Fall";
break;
case :
case :
case :
season "Winter";
break;
case :
case :
case :
season "Spring";
break;
break;
Output the result
cout "The season is season endl;
return ;
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