Question: The comments are the steps for the code #include #include using namespace std; //template for lab on dowhile and switch int main() { /* *

The comments are the steps for the code

#include  #include  using namespace std; //template for lab on dowhile and switch int main() { /* * Example #1: do while loop * TO-DO: Follow the steps in this example and write the appropriate code */ srand((unsigned)time(0)); int number, guess; char again; cout << "Play the guessing game "; //step2a: start a do while loop here number = rand() % 10 + 1; //generate a new random number cout << "I'm thinking of a number from 1 to 10. "; //step1a: start another do while loop here to repeatedly ask for guesses cout << "Try to guess it "; cin>> guess; while (guess < 1 || guess > 10 || cin.fail()) //validate guess 1-10 { cin.clear(); cin.ignore(80,' '); cout << "number must be 1-10 reenter "; cin >> guess; } if (number < guess) //check the guess cout << "too high! "; else if (number >guess) cout << "too low! "; else cout << "Correct! "; //step1b: end the dowhile loop if number answered correctly cout << "The number was " << number << endl; //step2b: ask the user if he/she wants to play again // store the reply in "again" // validate again (allow y or n), use tolower //step2c: end the dowhile loop if they dont want to play again /* * Example #2: Switch - what is printed if code is 458? * TO-DO: Once you've verified how the switch currently works, fix it so it works how you would expect */ int code; cout << " What is your program code? "; cin >> code; while (code < 0 || cin.fail()) { cin.clear(); cin.ignore(80, ' '); cout << "code cannot be negative - re enter"; cin >> code; } cout << " You are majoring in "; switch (code) //Note: can switch on int or char or double (not string) { //switch tests for equality only //can have 16,384 cases case 458: cout << "CPA"; case 457: cout << "CN&TS"; default: //default is optional cout << "unknown program entered"; } cout << endl; /* * Example #3: of switch  what is printed if F is entered? * TO-DO: Follow the steps in this example and write the appropriate code */ char gender; cout << "enter F/M "; cin >> gender; while (gender != 'F' && gender != 'f' && gender != 'M' && gender != 'm') { cout << "answer must be m or F - re enter "; cin >> gender; } cout << " Gender entered: "; switch (gender) { // Step 1: Allow both uppercase and lowercase to be entered case 'F': cout << "fe"; // Step 2: Move this outside of the switch case 'M': cout << "male"; } /* * Example #4: Rewrite nested if as a switch statement * TO-DO: Convert the if statement below to use a switch */ double gpa; char grade; cout << " What is your letter grade? "; cin >> grade; while (grade != 'A' && grade != 'B' && grade != 'C' && grade != 'D' && grade != 'F') { cout << "answer must be A,B,C,D or F - reenter "; cin >> grade; grade = toupper(grade); } if (grade == 'A') gpa = 4.0; else if (grade == 'B') gpa = 3.0; else if (grade=='C') gpa = 2.0; else if (grade=='D') gpa = 1.0; else gpa = 0.0; cout << " Your gpa is " << gpa << endl; cout << " End of examples " << endl; system("pause"); }

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!