In: Computer Science
Seasons Re-Visited
Write a program that contains an array of strings in main(). Your array of strings will contain the seasons, “Winter”, “Spring”, “Summer”, and “Fall”. Your program will then contain a function that will use your seasons array to display a menu and ask the user to choose their favorite season and return that value to the main() function. Your function must include a data validation loop. The user must enter in a 1 for winter, a 2 for spring, 3 for summer and 4 for fall.
Once you have returned the users numeric value back to main(), use your array and the users selection to display their favorite season.
I want the program to work but I need one or two mistake.
thanks for the question, here is a program that works but it contains two mistakes as you requested. I have commented the mistakes.
Let me know for any help or question.
=====================================================================
#include<iostream>
#include<string>
using namespace std;
int getSeason(){
int season ;
cout<<"Enter 1 for Winter, 2 for Spring, 3 for
Summer and 4 for Fall: ";
cin >> season;
// first mistake, we are checking the user entered a
valid number
// we simply returned the user number, user number can
be 0 or 100 or 1000
return season;
}
int main(){
string seasons[]
={"Winter","Spring","Summer","Fall"};
int season = getSeason();
// second mistake - we must subtract 1 from the user
value
// because the index of winter is 0, spring is 1,
summer is 2, and fall is 3
cout<<"Your favourite season is: " <<
seasons[season];
}
=====================================================================