C++- Please solve TODO sections and match sample output. TODO: (1) Demonstrate u
ID: 3904356 • Letter: C
Question
C++- Please solve TODO sections and match sample output.
TODO:
(1) Demonstrate using a Do-While loop in place of a While loop.
(2) Use a Switch statement in place of If-Else-If-Else.
*/
#include<iostream>
#include<iomanip>
using namespace std;
const char ANIMAL_KINGDOM = 'A'; // constants for menu options
const char EPCOT = 'E';
const char HOLLYWOOD_STUDIOS = 'H';
const char MAGIC_KINGDOM = 'M';
const char QUIT = 'Q';
int main()
{
char option; // input for menu choice
bool done; // flag for loop control
done = false;
// TODO: Convert this to a do-while loop
while (!done)
{
// display menu
cout << endl
<< setw(25) << " Program Menu " << endl
<< setw(25) << "---------------------" << endl
<< setw(5) << ANIMAL_KINGDOM << " ...... Animal Kingdom" << endl
<< setw(5) << EPCOT << " ...... EPCOT" << endl
<< setw(5) << HOLLYWOOD_STUDIOS << " ...... Hollywood Studios" << endl
<< setw(5) << MAGIC_KINGDOM << " ...... Magic Kingdom" << endl
<< setw(5) << QUIT << " ...... Quit Program" << endl
<< setw(25) << "---------------------" << endl;
// get user response
cout << setw(20) << "Enter your selection: ";
cin >> option;
option = toupper(option);
// TODO: convert this entire if...else if... to a switch statement
if (option == ANIMAL_KINGDOM) // option A
{
cout << " You are going to Animal Kingdom. Experience the "Kilamanjaro Safari"! ";
}
else if (option == EPCOT) // option E
{
cout << " You are going to EPCOT. "Try Fast Track" while you are there! ";
}
else if (option == HOLLYWOOD_STUDIOS) // option H
{
cout << " You are going to Hollywood Studios. "Rock and Roller Coaster" is awesome! ";
}
else if (option == MAGIC_KINGDOM) // option M
{
cout << " You are going to Magic Kingdom. Try the newest attraction, "7 Dwarves Mine Train Ride". ";
}
else if (option == QUIT) // option Q, set loop exit condition
{
done = true;
}
else // invalid menu response
{
cout << " Invalid selection, please try again. ";
}
} // end program loop
cout << " End Program - ";
}
/* Sample Output
Program Menu
---------------------
A ...... Animal Kingdom
E ...... EPCOT
H ...... Hollywood Studios
M ...... Magic Kingdom
Q ...... Quit Program
---------------------
Enter your selection: a
You are going to Animal Kingdom. Experience the "Kilamanjaro Safari"!
Program Menu
---------------------
A ...... Animal Kingdom
E ...... EPCOT
H ...... Hollywood Studios
M ...... Magic Kingdom
Q ...... Quit Program
---------------------
Enter your selection: E
You are going to EPCOT. "Try Fast Track" while you are there!
Program Menu
---------------------
A ...... Animal Kingdom
E ...... EPCOT
H ...... Hollywood Studios
M ...... Magic Kingdom
Q ...... Quit Program
---------------------
Enter your selection: h
You are going to Hollywood Studios. "Rock and Roller Coaster" is awesome!
Program Menu
---------------------
A ...... Animal Kingdom
E ...... EPCOT
H ...... Hollywood Studios
M ...... Magic Kingdom
Q ...... Quit Program
---------------------
Enter your selection: 3
Invalid selection, please try again.
Program Menu
---------------------
A ...... Animal Kingdom
E ...... EPCOT
H ...... Hollywood Studios
M ...... Magic Kingdom
Q ...... Quit Program
---------------------
Enter your selection: q
End Program - Press any key to continue . . .
*/
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
const char ANIMAL_KINGDOM = 'A'; // constants for menu options
const char EPCOT = 'E';
const char HOLLYWOOD_STUDIOS = 'H';
const char MAGIC_KINGDOM = 'M';
const char QUIT = 'Q';
int main()
{
char option; // input for menu choice
bool done; // flag for loop control
done = false;
// TODO: Convert this to a do-while loop
do
{
// display menu
cout << endl
<< setw(25) << " Program Menu " << endl
<< setw(25) << "---------------------" << endl
<< setw(5) << ANIMAL_KINGDOM << " ...... Animal Kingdom" << endl
<< setw(5) << EPCOT << " ...... EPCOT" << endl
<< setw(5) << HOLLYWOOD_STUDIOS << " ...... Hollywood Studios" << endl
<< setw(5) << MAGIC_KINGDOM << " ...... Magic Kingdom" << endl
<< setw(5) << QUIT << " ...... Quit Program" << endl
<< setw(25) << "---------------------" << endl;
// get user response
cout << setw(20) << "Enter your selection: ";
cin >> option;
option = toupper(option);
// TODO: convert this entire if...else if... to a switch statement
switch(option) {
case ANIMAL_KINGDOM: // option A
cout << " You are going to Animal Kingdom. Experience the "Kilamanjaro Safari"! ";
break;
case EPCOT: // option E
cout << " You are going to EPCOT. "Try Fast Track" while you are there! ";
break;
case HOLLYWOOD_STUDIOS: // option H
cout << " You are going to Hollywood Studios. "Rock and Roller Coaster" is awesome! ";
break;
case MAGIC_KINGDOM: // option M
cout << " You are going to Magic Kingdom. Try the newest attraction, "7 Dwarves Mine Train Ride". ";
break;
case QUIT: // option Q, set loop exit condition
done = true;
break;
default: // invalid menu response
cout << " Invalid selection, please try again. ";
}
} while (!done); // end program loop
cout << " End Program - ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.