I created a program that ask a user to pick a service package and tell them how much it is going to cost, what i cant figure out is how to get the program to tell them how much they would have saved if they had gone with a different package.


ill put comments in the code where im having problems, its near the bottom of main.

ive tried putting the different calculations in the actual package choice and at the end, i think they should go at the end but if not could someone guide me in the right direction.

#include <iostream>
#include<fstream>

using namespace std;


int main()
{
    cout << "Package A: $9.95 per month, 10 hours access, $2.00 per hour after that" << endl;
    cout << "Package B: $14.95 per month, 20 hours access, $1.00 per hour after that" << endl;
    cout << "Package C: $19.95 per month, unlimited acces" << endl << endl;

    char choice;


    double usage, charges, packA = 9.95, packB = 14.95, packC = 19.95, hoursA = 2.00,
            hoursB = 1.00, addCharge = 0.0, total = 0.0, savings1 = 0.0, savings2 = 0.0;


    string name;


    ofstream writeToFile;
    writeToFile.open("charges.txt");





//    cout << "What is your full name?" << endl;
//    getline(cin, name);


    cout << "Would you like package A, B or C?" << endl;
    cout << "Package: ";
    cin >> choice;


    cout << "How many hours did you use the internet?" << endl;
    cout << "Usage: ";
    cin >> usage;


    if ((choice == 'a') || (choice == 'A'))
    {

        charges = packA;
        cout << "\nMonthly Charge $" << charges << endl;

        if (usage > 10)
        {

            addCharge = (usage - 10) * hoursA;
            cout << "Additional charge $" << addCharge << endl;

        }
        total = charges + addCharge;
        cout << "Total Charges: $" << total << endl;
        
        

    }


    else if ((choice == 'b') || (choice == 'B'))
    {
        charges = packB;
        cout << "\nMonthly Charge $" << charges << endl;

        if (usage > 20)
        {
            addCharge = (usage - 20) * hoursB;
            cout << "Additional Charges: $" << addCharge << endl;
        }

        total = charges + addCharge;
        cout << "Total Charges: $" << total << endl;


    }


    else if ((choice == 'c') || (choice == 'C'))
    {
        charges = packC;
        cout << "\nTotal Charges: $" << charges << endl;
    }




// this is the part where im trying to output savings
     
    if (total > packA)
        {
            savings1 = total - packA;
            savings2 = total - packC;
            cout << "\nIf you had gone with package B you would have saved $" << savings1 << endl;
            
            //nested if else to stop a negative savings from appearing
            if (savings2 < 0)
            {
                cout << "";
            }

            else
            {
                cout << "\nIf you had gone with package C you would have saved $" << savings2 << endl;
            }



        }


    else 
    {
        savings2 = total - packB;
        cout << "If you went with package C you would have saved $" << savings2 << endl;

    }

    // end of problem with savings area




    writeToFile << name << endl;
    writeToFile << choice << endl;
    writeToFile << usage << endl;
    writeToFile << total << endl;

    writeToFile.close();



    return 0;
}

To me your second part makes close to no sense at all.

Assume we have chosen package A and have used it for 22 hours. The cost from package A will then be 9.95+24. So 33.95 - this part you calculate correctly as far as I can see.
But when it comes to savings I see several wrong things.

if (total > packA)
        {
            savings1 = total - packA;
            savings2 = total - packC;
            cout << "\nIf you had gone with package B you would have saved $" << savings1 << endl;
...
}

If you wanted to show the savings if he had used package B, why do you subtract package A from the total? It would appear to make more sense to subtract the cost from package B. And furthermore, you totally neglect the amount of hours used. You just consider the basic package costs.

So with the above numbers used package B costs me: 14.95+2 = 16.95.

What you could do is to calculate each package cost separately and save them in distinguished variables and at the end you take the differences and only output those where you would have saved money by choosing a different package.

I hope I didn't make any mistakes, I worked nightshift last night and only slept 3hours.

what your saying makes sense, i started to mess with using your suggestions a little but its almost 2am where i am and like you i am a little. i will sleep on it and work on it later, technically later today lol, but will post if i have any more problems or if i figured it out. THanks for your advice though.

To me your second part makes close to no sense at all.

Assume we have chosen package A and have used it for 22 hours. The cost from package A will then be 9.95+24. So 33.95 - this part you calculate correctly as far as I can see.
But when it comes to savings I see several wrong things.

if (total > packA)
        {
            savings1 = total - packA;
            savings2 = total - packC;
            cout << "\nIf you had gone with package B you would have saved $" << savings1 << endl;
...
}

If you wanted to show the savings if he had used package B, why do you subtract package A from the total? It would appear to make more sense to subtract the cost from package B. And furthermore, you totally neglect the amount of hours used. You just consider the basic package costs.

So with the above numbers used package B costs me: 14.95+2 = 16.95.

What you could do is to calculate each package cost separately and save them in distinguished variables and at the end you take the differences and only output those where you would have saved money by choosing a different package.

I hope I didn't make any mistakes, I worked nightshift last night and only slept 3hours.

A suggestion if I may...
Create a function that takes
1) Package
2) Usage
and returns Cost

Then you can call the function at the top and display the usage cost.

For part 2, call the function twice more with the other two Package values and you can subtract the costs from the Cost above to check for savings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.