Question: 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 (1 for January, 2 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): Dec-Feb
Spring (NH), Fall (SH): Mar-May
Summer (NH), Winter (SH): Jun- Aug
Fall (NH), Sprint (SH): Sep-Nov
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 1 or greater than 12 for the month, display the message "Please enter a valid month number (1-12)."
** Sample Runs**: User input is in between square brackets.
Enter a hemisphere (N - S): [N]
Enter a month number (1-12): [3]
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 (1-12): [13]
Please enter a valid month number (1-12).
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 = toupper(hemisphere); // 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 1; // Exit the program with an error code
}
// Input for month
cout << "Enter a month number (1-12): ";
cin >> month;
// Input validation for month
if (month <1|| month >12){
cout << "Please enter a valid month number (1-12)."<< endl;
return 1; // Exit the program with an error code
}
// Determine the season based on hemisphere and month
switch (hemisphere){
case 'N': // Northern Hemisphere
switch (month){
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
case 11:
season = "Fall";
break;
}
break;
case 'S': // Southern Hemisphere
switch (month){
case 12:
case 1:
case 2:
season = "Summer";
break;
case 3:
case 4:
case 5:
season = "Fall";
break;
case 6:
case 7:
case 8:
season = "Winter";
break;
case 9:
case 10:
case 11:
season = "Spring";
break;
}
break;
}
// Output the result
cout << "The season is "<< season <<"."<< endl;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!