0

Im trying to complete exercises out of a c++ book that does not provide solutions. The first part was to just ask for internet package choice and output the cost of it as well as any additional fees.


The part im having trouble with is:

"Modify the program to show how much money Package A customers would save if they purchased packages B or C
and how much money package B customers would save if they purchased package C. If there is no savings no message should be given.


I think i should do it at the end of the if statements but am not sure and was looking for input or ideas.

Code from part one:

/* 
 * File:   main.cpp
 * 
 *
 * Created on July 6, 2011, 10:27 PM
 */

#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;
    
    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 > 10)
        {
            addCharge = (usage - 10) * 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;
    }



    
    
    /*should the stuff for part 2 go here?*/
    
    


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

    writeToFile.close();



    return 0;
}

Edited by clickspiker23: n/a

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Saith
0

Sure, you can add the second part after the calculations.

Prior to that, you may want to slightly edit what you currently have.

I'm assuming the code below is suppose to be read as this:

else if ((choice == 'b') || (choice == 'B'))
{
charges = packB;
cout << "\nMonthly Charge $" << charges << endl;
 
if (usage > 10)         // !!! 20, not 10 as there are 20 free hours in B, 10 in A
{
addCharge = (usage - 10) * hoursB;         // !!! make the same change here
cout << "Additional Charges: $" << addCharge << endl;
}
 
total = charges + addCharge;
cout << "Total Charges: $" << total << endl;
 
 
}

As it is, you may as well make a function as choice A and choice B are very similar. It cuts back on the repeated typed code. You would likely send in the -usage difference- (10 for A, 20 for B), -usage- (total number of hours).

And with the function, you may as well send call both A and B if choice A is made, or just send B if choice B is called and have a return type of double. Once the function is finished, you can output the results of the chosen selection, then compare each returned type.

You may find that you should compare A and B with choice C and A with choice B as imagine if a person only uses an hour or two of an internet package.

returnedChoiceA = ChoiceA(usage, difference);
returnedChoiceB = ChoiceB(usage, difference);
returnedChoiceC = packC;

if(choice equals A or a)
 output << Additional Charges
 output << Total Charges
 output << savings vs B vs C (if applicable)

else if (choice equals B or b)
 output << Additional Charges
 output << Total Charges
 output << savings vs A vs C (if applicable)
else
 output << packC
 output << savings vs A vs B (if applicable)

And even that can be shoved into another function, if desired.


Just my input, I know there will be a hundred better ways of doing this.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.