Question: Replace ALL ifs with a MAXIMUM of TWO switch statements Program should produce same exact results #include #include #include // srand, rand #include //time using
Replace ALL ifs with a MAXIMUM of TWO switch statements Program should produce same exact results
#include#include #include // srand, rand #include //time using namespace std; string weatherString(int); //Precondition: Expects and integer between 0 and 5, anything else will be GLOOMY //Postcondition: A string explaining the CURRENT weather is returned int main() { char selection; int weatherNumber; //initialize random generator srand (time(NULL)); //Set the current weather randomly weatherNumber = rand() % 7; cout << "--==[ Welcome to Our Menu ]==--" << endl << endl << "Select and option:" << endl << " 1,w) Give current weather" << endl << " 2,r) Make it rain" << endl << " 3,j) Tell a joke" << endl << " 4,q,a) Quit/Abort" << endl; cin >> selection; if ( selection == '1' || selection == 'w' || selection == 'W' || selection == '2' || selection == 'r' || selection == 'R' ) { if ( selection == '2' || selection == 'r' || selection == 'R' ) weatherNumber = 0; //0 is RAIN, user wants to make it rain cout << weatherString(weatherNumber) << endl; } else if ( selection == '3' || selection == 'j' || selection == 'J' ) cout << "Get ready to laugh....." << endl << endl << "The generation of random numbers is too important to be left to chance." << endl; else if ( selection == '4' || selection == 'a' || selection == 'A' || selection == 'q' || selection == 'Q' ) cout << "aborted" << endl; else cout << "ERROR: invalid option." << endl; return 0; } string weatherString(int w) { const int iindx = 10; //Insert index string wstr = "It is now ."; if ( w == 0 ) wstr.insert(iindx,"raining"); else if ( w == 1 ) wstr.insert(iindx,"snowing"); else if ( w == 2 ) wstr.insert(iindx,"sunny"); else if ( w == 3 ) wstr.insert(iindx,"foggy"); else if ( w == 4 ) wstr.insert(iindx,"partly cloudy"); else if ( w == 5 ) wstr.insert(iindx,"cloudy"); else wstr.insert(iindx,"gloomy"); return wstr; }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
