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...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify the given part of the program. Run your program against the provided Homework2Driver.java for requirements. */ /* Hint: This Queue implementation will always dequeue from the first element of the array i.e, elements[0]. Therefore, remember to shift all elements toward front of the queue after each dequeue. */ public class QueueArray<T> { public static int CAPACITY = 100; private final T[] elements; private int...
MODIFY your code to: add a new point, locationx, that has 3 dimensions, and are set...
MODIFY your code to: add a new point, locationx, that has 3 dimensions, and are set by PASSING IN the arguments for the X and Y coordinates. The Z coordinate can be hard-coded (extra credit if it is passed in too). ADD CODE to print the new point location, and note to the user it is the new point using input arguments Add 2 new methods to the Point3d class, each accepting 3 values as input. pAdd(a1, a2, a3) will...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to double to cost of the ticket in a construction zone. // This project will calculate a speeding ticket between 0 to 150 mph. // 1. Ask for speed. // 2. Input speed of vehicle // 3. Calculate ticket cost (50$ if over 50mph with an additional 5$ for every mph over). // 4. Display cost of ticket. #include<iostream> using namespace std; int main() {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT