We are given the following code and must modify it. The following are the instru
ID: 3559094 • Letter: W
Question
We are given the following code and must modify it. The following are the instructions, and below the instructions is the code.
// This menu-driven Health Club membership program carries out the
// appropriate actions based on the menu choice entered. A do-while loop
// allows the program to repeat until the user selects menu choice 4.
#include <iostream>
#include <iomanip>
using namespace std;
// Constants for membership rates
const double ADULT_RATE = 40.0;
const double CHILD_RATE = 20.0;
const double SENIOR_RATE = 30.0;
void displayMenu();
void verifyChoice(int &);
void processChoice(int &);
int main()
{
int choice; // Menu choice
do
{
displayMenu();
cin >> choice;
verifyChoice(choice);
processChoice(choice);
} while (choice != 4); // Loop again if the user did not
system("pause"); // select choice 4 to quit
return 0;
}
void displayMenu(){
// Display the menu and get the user's choice
cout << " Health Club Membership Menu ";
cout << "1. Standard Adult Membership ";
cout << "2. Child Membership ";
cout << "3. Senior Citizen Membership ";
cout << "4. Quit the Program ";
cout << "Enter your choice: ";
}
void verifyChoice(int &choice)
{
// Validate the menu selection
while ((choice < 1) || (choice > 4))
{
cout << "Please enter 1, 2, 3, or 4: ";
cin >> choice;
}
}
void processChoice(int &choice)
{
int months; // Number of months
double charges; // Monthly charges
// Process the user's choice
if (choice != 4)
{ cout << "For how many months? ";
cin >> months;
// Compute charges based on user input
switch (choice)
{
case 1: charges = months * ADULT_RATE;
break;
case 2: charges = months * CHILD_RATE;
break;
case 3: charges = months * SENIOR_RATE;
}
// Display the monthly charges
cout << fixed << showpoint << setprecision(2);
cout << "The total charges are $" << charges << endl;
}
}
Explanation / Answer
// This menu-driven program uses a switch statement to carry out // the appropriate set of actions based on the menu choice entered. #include #include using namespace std; int main() { // Constants for membership rates const double ADULT_RATE = 40.0; const double CHILD_RATE = 20.0; const double SENIOR_RATE = 30.0; int choice; // Menu choice int months; // Number of months double charges; // Monthly charges // Display the menu and get the user's choice coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.