//This is a modification of the textbook health club examples //that calculate dues for members in three different categories. //It illustrates breaking a program into functions that each perform //specific tasks. #include #include #include using namespace std; int displayMenuandChoose(); double processMenuChoice( int choice); double calculateDues(int months, double rate ); void printDues(double charges); void printDuesInfotoFile( ofstream &fout, int choice, double charges); // Global Constants for menu choices const int ADULT_CHOICE = 1, CHILD_CHOICE = 2, SENIOR_CHOICE = 3, QUIT_CHOICE = 4; // Global Constants for membership rates const double ADULT = 40.0, CHILD = 20.0, SENIOR = 30.0; int main() { int choice; // Menu choice double charges; // Monthly charges ofstream fout; fout.open("CLUBDUESINFO.TXT"); do { choice = displayMenuandChoose(); charges = processMenuChoice(choice); if ( charges > 0) { printDues(charges); printDuesInfotoFile(fout, choice, charges); } } while (choice !=QUIT_CHOICE); fout.close(); return 0; } int displayMenuandChoose() { int choice; // Display the menu. cout << "\n\t\tHealth Club Membership Menu\n\n" << "1. Standard Adult Membership\n" << "2. Child Membership\n" << "3. Senior Citizen Membership\n" << "4. Quit the Program\n\n" << "Enter your choice: "; cin >> choice; return choice; } //end displayMenuandChoose() //Function processes user's menu entry and validates entry //Calls a computation function that assesses monthly dues and returns those dues. //As written, function cannot return the number of months of membership double processMenuChoice( int choice) { int months; double dues; switch (choice) { case ADULT_CHOICE: cout << "For how many months? "; cin >> months; while (months < 3) { cout << "Sorry, but you must join for at least 3 months\n"; cout << "Try again \n"; cin >> months; } dues = calculateDues(months, ADULT); break; case CHILD_CHOICE: cout << "For how many months? "; cin >> months; while(months < 3) { cout << "Sorry, but you must join for at least 3 months\n"; cout << "Try again \n"; cin >> months; } dues = calculateDues(months, CHILD); break; case SENIOR_CHOICE: cout << "For how many months? "; cin >> months; while (months < 6) { cout << "Sorry, but you must join for at least 6 months\n"; cout << "Try again \n"; cin >> months; } dues = calculateDues(months, SENIOR); break; default: cout << "You did not enter a valid menu choice \n"; dues = 0; break; } //end switch return dues; } //end Process menu choice //Based on months of membership functions calculates dues //this function could well be more complex and use more parameters //to determine dues //for example, you might have a discounted rate or a special offering rate //for a personal trainer depending on member classification double calculateDues(int months, double rate ) { double dues; dues = months*rate; cout << "Months are " << months << endl; cout << "Dues in calculateDues are " << dues << endl; return dues; } //simple screen display function void printDues(double charges) { cout << fixed << showpoint << setprecision(2); cout << "The total charges are $" << charges << endl; } //This function is a stub void printDuesInfotoFile(ofstream &fout, int choice, double charges) { cout << "I am in PrintDuesInfotoFile \n"; }