Replicate this Program, but substitute the Switch with Nested If statements // T
ID: 3564268 • Letter: R
Question
Replicate this Program, but substitute the Switch with Nested If statements
// This program uses a switch statement to determine
// the item selected from a menu.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int choice; // To hold a menu choice
int months; // To hold the number of months
double charges; // To hold the monthly charges
// Constants for membership rates
const double ADULT = 40.0,
CHILD = 20.0,
SENIOR = 30.0;
// Constants for menu choices
const int ADULT_CHOICE = 1,
CHILD_CHOICE = 2,
SENIOR_CHOICE = 3,
QUIT_CHOICE = 4;
// Display the menu and get a choice.
cout << " Health Club Membership Menu "
<< "1. Standard Adult Membership "
<< "2. Child Membership "
<< "3. Senior Citizen Membership "
<< "4. Quit the Program "
<< "Enter your choice: ";
cin >> choice;
// Set the numeric output formatting.
cout << fixed << showpoint << setprecision(2);
// Respond to the user's menu selection.
switch (choice)
{
case ADULT_CHOICE:
cout << "For how many months? ";
cin >> months;
charges = months * ADULT;
cout << "The total charges are $" << charges << endl;
break;
case CHILD_CHOICE:
cout << "For how many months? ";
cin >> months;
charges = months * CHILD;
cout << "The total charges are $" << charges << endl;
break;
case SENIOR_CHOICE:
cout << "For how many months? ";
cin >> months;
charges = months * SENIOR;
cout << "The total charges are $" << charges << endl;
break;
case QUIT_CHOICE:
cout << "Program ending. ";
break;
default:
cout << "The valid choices are 1 through 4. Run the "
<< "program again and select one of those. ";
}
return 0;
}
Explanation / Answer
// the item selected from a menu.
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int choice; // To hold a menu choice
int months; // To hold the number of months
double charges; // To hold the monthly charges
// Constants for membership rates
const double ADULT = 40.0,CHILD = 20.0,SENIOR = 30.0;
// Constants for menu choices
const int ADULT_CHOICE = 1,CHILD_CHOICE = 2,SENIOR_CHOICE = 3,QUIT_CHOICE = 4;
// Display the menu and get a choice.
cout << " Health Club Membership Menu "
<< "1. Standard Adult Membership "
<< "2. Child Membership "
<< "3. Senior Citizen Membership "
<< "4. Quit the Program "
<< "Enter your choice: ";
cin >> choice;
// Set the numeric output formatting.
cout << fixed << showpoint << setprecision(2);
// Respond to the user's menu selection.
if(choice == 1){
cout << "For how many months? ";
cin >> months;
charges = months * ADULT;
cout << "The total charges are $" << charges << endl;
}
else if(choice == 2){
cout << "For how many months? ";
cin >> months;
charges = months * CHILD;
cout << "The total charges are $" << charges << endl;
}
else if(choice == 3){
cout << "For how many months? ";
cin >> months;
charges = months * SENIOR;
cout << "The total charges are $" << charges << endl;
}
else if(choice == 4){
cout << "Program ending. ";
}
else{
cout << "The valid choices are 1 through 4. Run the "
<< "program again and select one of those. ";
}
return 0;
}