Question

In: Computer Science

Modify program 4-27 on page 219 as follows: 1) Add a new membership category for STUDENTS...

Modify program 4-27 on page 219 as follows:

1) Add a new membership category for STUDENTS with a monthly rate of $90.00. The menu should have one more choice, and the switch statement should implement one more case.

2) The input for the number of months should be validated for 1 -36 months. No charges should be displayed for incorrect number of months. Instead, an error should be displayed similar with the one for wrong menu choice.

// This menu-driven program uses a switch statement to carry out

// the appropriate set of actions based on the user's menu choice.

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

// Constants for membership rates

const double ADULT_RATE = 120.0;

const double CHILD_RATE = 60.0;

const double SENIOR_RATE = 100.0;

int choice; // Menu choice

int months; // Number of months

double charges; // Monthly charges

// Display the menu and get the user's choice

cout << " Health Club Membership Menu\n\n";

cout << "1. Standard Adult Membership\n";

cout << "2. Child Membership\n";

cout << "3. Senior Citizen Membership\n";

cout << "4. Quit the Program\n\n";

cout << "Enter your choice: ";

cin >> choice;

// Validate and process the menu choice

if (choice >= 1 && choice <= 3)

{ cout << "For how many months? ";

cin >> months;

// Set 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;

}

else if (choice != 4)

{ cout << "The valid choices are 1 through 4.\n";

cout << "Run the program again and select one of these.\n";

}

return 0;

}

Solutions

Expert Solution

Solution :

  • Declare a constant double STUDENT_RATE equal to $90.0
  • In the Switch case, add one more case for student membership.
  • Validate months to be in the range 1 to 36.
  • Add an error message using else statement, if entered month is not in the range 1 - 36

Following is the program for the same :

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

// Constants for membership rates

const double ADULT_RATE = 120.0;

const double CHILD_RATE = 60.0;

const double SENIOR_RATE = 100.0;

const double STUDENT_RATE = 90.0;

int choice; // Menu choice

int months; // Number of months

double charges; // Monthly charges

// Display the menu and get the user's choice

cout << " Health Club Membership Menu\n\n";

cout << "1. Standard Adult Membership\n";

cout << "2. Child Membership\n";

cout << "3. Senior Citizen Membership\n";

cout << "4. Student Membership\n";

cout << "5. Quit the Program\n\n";

cout << "Enter your choice: ";

cin >> choice;

// Validate and process the menu choice

if (choice >= 1 && choice <= 4)

{ cout << "For how many months? ";

cin >> months;

// Validate and process no. of months

if(months >=1 && months <= 36)

{
    
// Set 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;

break;

case 4: charges = months * STUDENT_RATE;

}

// Display the monthly charges

cout << fixed << showpoint << setprecision(2);

cout << "The total charges are $" << charges << endl;

}

else cout<<"The valid choices are 1 through 36.\n"; 

}

else if (choice != 4)

{ cout << "The valid choices are 1 through 4.\n";

cout << "Run the program again and select one of these.\n";

}

return 0;

}

Code demo :

Output 1:

Output 2 :


Related Solutions

Modify the program 7-5 (pr7-5.cpp) on page 425 by performing the following: 1. Add an overloaded...
Modify the program 7-5 (pr7-5.cpp) on page 425 by performing the following: 1. Add an overloaded constructor that has a parameter for radius. Negative values should result in radius set to 1.0. (see example 7-6 page 427) 2. Add a member function calcCircumference() to compute and return the circle circumference (2 * 3.14 * radius). 3. In main() "circle1" should be instantiated with a value for radius. // This program uses a constructor to initialize a member variable. #include <iostream>...
In C++ please modify the following program and add characters (char) and names (strings) to be...
In C++ please modify the following program and add characters (char) and names (strings) to be added to the linked list along with integers. The current demo program accepts only integer data, so you would ask the user to select the data type to be added to the linked list. The user should be given the following three choices: (a) whole numbers (b) single characters (c) strings Once the user makes a selection from the list above then your program...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be sure to include start() and stop() methods to start and stop the clock, respectively.Then write a program that lets the user control the clock with the start and stop buttons.
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){ localSum+=...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when the program runs. Have the program request the number of initial numbers to be entered." //C++ Program 7.14 as follows #include #include #include #include using namespace std; int main() { const int NUMELS = 4; int n[] = {136, 122, 109, 146}; int i; vector partnums(n, n + NUMELS); cout << "\nThe vector initially has the size of " << int(partnums.size()) << ",\n and...
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
Modify the program 5-13 from page 279 such that will also compute the class average. This...
Modify the program 5-13 from page 279 such that will also compute the class average. This class average is in addition to each individual student score average. To accomplish this additional requirement, you should do the following: 1. Add two more variables of type double: one for accumulating student averages, and one to hold the class average. Don't forget, accumulator variable should be initialized to 0.0. 2. Immediately after computing individual student average, add a statement that will accumulate the...
Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java...
Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java * description of class Driver here. *these is the main class where it acts like parent class. Compiler will execute firstly static method. import java.util.Scanner; public class Driver {     public static void main (String[] args){         Scanner stdIn = new Scanner(System.in);         String user;         String computer;         char a = 'y';         do {             System.out.println("What kind of Computer would you like?");...
A campus student club distributed material about membership to new students attending an orientation meeting. Of...
A campus student club distributed material about membership to new students attending an orientation meeting. Of those receiving this material, 35​% were men and 65​% were women.​ Subsequently, it was found that 8​% of the men and 11​% of the women who received this material joined the club. Complete parts a and b below. a. Find the probability that a randomly chosen new student who receives the membership material will join the club. nothing ​(Round to four decimal places as​...
4. Modify the program geometryDemo to use the class basicGeometry and the threeSides code. Note from...
4. Modify the program geometryDemo to use the class basicGeometry and the threeSides code. Note from the comments in the main routine that it wants an input argument of 3 to solve for a triangle and 4 to solve for a square or rectangle. MODIFY the code to add another method in the 'threeSides' and 'fourSides' classes to return the perimeter. ASSUME the following for TRIANGLES. ** for AREA (1/2 * base * height) dimension1 is base, dimension2 is height...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT