-5

I am working on this exercise and it is pretty much complete but the math has to be wrong somewhere when calculating the senior citizen discount. The software that I use runs these two pieces of input to check the problem.

  • 20 10 n 2 12 (which runs fine)

  • 20 10 y 2 12 (does not not give me expected result)

This leads me to believe that the problem lies within the senior citizen discount part in the double determineMembershipCost function.

The expected result is "The membership cost = $162.80" but my code gives me "The membership cost = $152.00"

I am not sure what is wrong here. I'm hoping a second set of eyes can help find it. Thank you in advance.

Here is the code:

// headers
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// prototypes
void displayGeneralInformation ();
void readNecessaryInformation (double& regularCostPerMonth,
                               double& costPerPersonalTrainingSession,
                               bool& seniorCitizen, int& numberOfSessions,
                               int& numberOfMonths);
double determineMembershipCost (double regularCostPerMonth,
                                double costPerPersonalTrainingSession,
                                bool seniorCitizen, int numberOfSessions,
                                int numberOfMonths);

// main
int main()
{
    //variables
    double regularCostPerMonth;
    double costPerPersonalTrainingSession;
    bool seniorCitizen;
    int numberOfSessions;
    int numberOfMonths;
    double cost;

    // print menu

    // calls

    // call displayGeneralInformation
    displayGeneralInformation ();
    // call readNecessaryInformation
    readNecessaryInformation (regularCostPerMonth,
                              costPerPersonalTrainingSession,
                              seniorCitizen, numberOfSessions,
                              numberOfMonths);
    // call determineMembershipCost
    cost = determineMembershipCost (regularCostPerMonth, costPerPersonalTrainingSession, seniorCitizen, numberOfSessions,                                                       numberOfMonths);
    // Display cost of membership
    cout << "\nThe membership cost = $" << setprecision(2)<< fixed << cost << endl; 

    return 0;
}

// displayGeneralInformation function definition

void displayGeneralInformation ()
{
    cout << "\nWelcome to Stay Healthy and Fit center." << endl;
    cout << "This program determines the cost of a new membership." << endl;
    cout << "If you are a senior citizen, then the discount is 30% off of the regular membership price." << endl;
    cout << "If you buy membership for twelve months and pay today, the discount is 15%." << endl;
    cout << "If you buy and pay for 6 or more personal training session today, the discount on each session is 20%." << endl;
}

// readNecessaryInformation function definition

void readNecessaryInformation (double& regularCostPerMonth,
                              double& costPerPersonalTrainingSession,
                              bool& seniorCitizen, int& numberOfSessions,
                              int& numberOfMonths)
{
    cout << "\nEnter the cost of a regular membership per month: $";
    cin >> regularCostPerMonth;

    cout << "Enter the cost of one personal training session: $";
    cin >> costPerPersonalTrainingSession;

    cout << "Are you a senior citizen (Y,y/N,n): ";
    char ch;
    cin >> ch;
    if (ch == 'Y' || ch == 'y')
        seniorCitizen = true;
    else
        seniorCitizen = false;

    cout << "Enter the number of personal training sessions bought: ";
    cin >> numberOfSessions;

    cout << "Enter the number of months you are paying for: ";
    cin >> numberOfMonths;
}

// determineMembershipCost function definition

double determineMembershipCost (double regularCostPerMonth, double costPerPersonalTrainingSession, bool seniorCitizen, int numberOfSessions, int numberOfMonths)
{
    double cost = regularCostPerMonth * numberOfMonths;

    if (seniorCitizen)
    {
        cost = cost - (regularCostPerMonth * 0.30 * numberOfMonths);
    }

    if (numberOfMonths >= 12)
    {
        cost = cost - (regularCostPerMonth * 0.15 * numberOfMonths);
    }

    cost = cost + (costPerPersonalTrainingSession * numberOfSessions);

    if (numberOfSessions > 5)
    {
        cost = cost - (costPerPersonalTrainingSession * 0.20 * numberOfSessions);
    }

    return cost;
}
Dev
  • 33
  • 1
  • 7
  • Have you tried debugging the code ? – Atul Kumar Feb 18 '19 at 04:17
  • @Atul Kumar Yes and it compiles and runs just fine. Something with the math has to be wrong I think. – Dev Feb 18 '19 at 04:19
  • 1
    You might want to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) for that or look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Feb 18 '19 at 04:20
  • My IDE has a built-in debugger and it does not pick anything up. – Dev Feb 18 '19 at 04:27
  • In many cases, debuggers won't just magically tell you what your bug is. Instead, *actually read the pages linked to by Andreas*, and use the debugger to step through your code. – eesiraed Feb 18 '19 at 05:33

1 Answers1

1

Try this instead:

cost = (cost - (cost * 0.30)); //for 30% off
cost = (cost - (cost * 0.15)); //15% off
donpsabance
  • 358
  • 3
  • 9