Well, everyone, here I am again. (I'm becoming a familiar face, am I???):confused:
I am working on an program in Borland C++ Builder 6 in which I have to create a program that asks the user for their name, what package they choose, and how many hours did they use. (Keep in mind that this program is not completed.)
Well, I have the program, and here it is.

#include <iostream>
#include <iomanip>      //Needed for the showpoint and setprecison command
#include <fstream>      //Needed to use files
#include <string>
#include <conio>        //Needed to show black output screen
using namespace std;
int main()
{
        char choice;
        int hours;
        string name;
        double charges;
 // Displays the menu choices  on the screen.
        cout << "\t\tInternet Service Provider\n";
        cout << "\t\tSubscription Packages\n\n";
 cout << "A: For $9.95 per month, can get 10 hours of\n";
        cout << "   access. Additional hours are $2.00 per hour. \n\n" ;
        cout << "B: For $14.95 per month, you can get 20 hours\n";
        cout << "   of access. Additonal hours are $1.00 per hour. \n\n";
        cout << "C: For $19.95 per month unlimited access is provided.\n\n";
        cout << "Please enter your name. ";
        getline (cin, name);
        cout << "Which subscription package would you like?\n";
        cin.get(choice);
        cout << fixed <<showpoint <<setprecision(2);
        if (choice == 'A' || 'a')
        {
                charges = 9.95 + (
                cout << "Your charges are $ " << charges << endl;
        }
        else if (choice == 'B'||'b')
        {
                charges =
                cout << "Your charges are $ " << charges << endl;
        }
        else if (choice == 'C' || 'c')
        {
                charges =
                cout << "Your charges are $ " << charges << endl;
        }
        else if (choice > 'C' || >744)
        {       cout << "You must choose packages A, B, or C. Also, your hours\n";
                cout << "must not exceed 744.\n";
        }
        getch();
 return 0;
}

Here is my problem:
I am trying to find out what calculations to use on the charges line.
The choices are:
A- $9.95 a month 10 hours are provided. Additional hours are $2.00 per hour.
B. $14.95 a month 20 hours are provided. Additonal hours are $1.00 per hour.
C. $19.95 per month. Unlimted access is provided.

For example, if a user chooses package A, and they use 15 hours that month, then that user would pay the $9.95 charge (for the 10 hours) plus an additional $10.00 ($2.00 * $5.00), which would be $19.95. I know how to calculate it, I just need help figuring out how to write it in C++.

Any input is appreciated. :p :cool:

Recommended Answers

All 11 Replies

Use variables.
int numberOfHoursUsed;
int baseHours;
double excessFee;
double baseFee;
double totalFee;
double overchargeRate;
int overchargeTime;

baseHours is given for a given program
basFee is given for a given program
overchargeRate is given for a given program
overchargeTime is numberOfHoursUsed minus baseHours
excessFee = overchargRate times overchargeTime
totalfee = baseFee plus excessFee;

Well, I did that, and this is what happened:

#include <iostream>
#include <iomanip>      //Needed for the showpoint and setprecison command
#include <fstream>      //Needed to use files
#include <string>
#include <conio>        //Needed to show black output screen
using namespace std;
int main()
{
        char choice;
        int numberofHoursUsed, baseHours, overchargeTime;
        string name;
        double excessFee, baseFee, overchargeRate, totalFee;
 // Displays the menu choices  on the screen.
        cout << "\t\tInternet Service Provider\n";
        cout << "\t\tSubscription Packages\n\n";
 cout << "A: For $9.95 per month, can get 10 hours of\n";
        cout << "   access. Additional hours are $2.00 per hour. \n\n" ;
        cout << "B: For $14.95 per month, you can get 20 hours\n";
        cout << "   of access. Additonal hours are $1.00 per hour. \n\n";
        cout << "C: For $19.95 per month unlimited access is provided.\n\n";
        cout << "Please enter your name. ";
        getline (cin, name);
        cout << "Which subscription package would you like? ";
        cin.get(choice);
 
        cout << fixed <<showpoint <<setprecision(2);
 
        if (choice == 'A' || 'a')
        {
                cout << "How many hours did you use? ";
                cin  >> numberofHoursUsed;
                baseHours = 10;
                baseFee = 9.95;
                overchargeRate = 2.00;
                overchargeRate = numberofHoursUsed - baseHours;
                excessFee = overchargeRate * overchargeTime;
                totalFee = baseFee + excessFee;
                cout << "Your charges are $ " << totalFee << endl;
        }
        else if (choice == 'B' || 'b')
        {
                cout << "How many hours did you use? ";
                cin  >> numberofHoursUsed;
                baseHours = 20;
                baseFee = 14.95;
                overchargeRate = 1.00;
                overchargeRate = numberofHoursUsed - baseHours;
                excessFee = overchargeRate * overchargeTime;
                totalFee = baseFee + excessFee;
                cout << "Your charges are $ " << totalFee << endl;
        }
        else if (choice == 'C' || 'c')
        {
                cout << "How many hours did you use? ";
                cin  >> numberofHoursUsed;
                baseHours = 0;
                baseFee = 19.95;
                overchargeRate = 0;
                overchargeRate = numberofHoursUsed - baseHours;
                excessFee = overchargeRate * overchargeTime;
                totalFee = baseFee + excessFee;
                cout << "Your charges are $ " << totalFee << endl;
        }
        else if (choice !='C')
        {       cout << "You must choose packages A, B, or C. Also, your hours\n";
                cout << "must not exceed 744.\n";
        }
        getch();
 return 0;
}

Output line:
Internet Service Provider
Subscription Packages
A: For $9.95 per month, can get 10 hours of
access. Additional hours are $2.00 per hour.
B: For $14.95 per month, you can get 20 hours
of access. Additonal hours are $1.00 per hour.
C: For $19.95 per month unlimited access is provided.
Please enter your name. Nadia A. McIntosh
Which subscription package would you like? A
How many hours did you use? 15
Your charges are $ 63341909.95:?:
Why did my number come out like that?? What did I do wrong??

Because the government added tax to the calculation.... :p

Actually, because you never calculated overchargeTime. You mistyped the equation for that calculation.

Okay, I got that problem solved, but I ran into another problem:

#include <iostream>
#include <iomanip>      //Needed for the showpoint and setprecison command
#include <fstream>      //Needed to use files
#include <string>
#include <conio>        //Needed to show black output screen
using namespace std;
int main()
{
        char choice;
        int numberofHoursUsed, baseHours, overchargeTime;
        string name;
        double excessFee, baseFee, overchargeRate, totalFee;
 // Displays the menu choices  on the screen.
        cout << "\t\tInternet Service Provider\n";
        cout << "\t\tSubscription Packages\n\n";
 cout << "A: For $9.95 per month, can get 10 hours of\n";
        cout << "   access. Additional hours are $2.00 per hour. \n\n" ;
        cout << "B: For $14.95 per month, you can get 20 hours\n";
        cout << "   of access. Additonal hours are $1.00 per hour. \n\n";
        cout << "C: For $19.95 per month unlimited access is provided.\n\n";
        cout << "Please enter your name. ";
        getline (cin, name);
        cout << "Which subscription package would you like? ";
        cin.get(choice);
        
        cout << fixed <<showpoint <<setprecision(2);

        if (choice == 'A' || 'a')
        {
                cout << "How many hours did you use? ";
                cin  >> numberofHoursUsed;
                baseHours = 10;
                baseFee = 9.95;
                overchargeRate = 2.00;
                overchargeTime = numberofHoursUsed - baseHours;
                excessFee = overchargeRate * overchargeTime;
                totalFee = baseFee + excessFee;
                cout << "Your charges are $ " << totalFee << endl;
        }
        else if (choice == 'B' || 'b')
        {
                cout << "How many hours did you use? ";
                cin  >> numberofHoursUsed;
                baseHours = 20;
                baseFee = 14.95;
                overchargeRate = 1.00;
                overchargeTime = numberofHoursUsed - baseHours;
                excessFee = overchargeRate * overchargeTime;
                totalFee = baseFee + excessFee;
                cout << "Your charges are $ " << totalFee << endl;
        }
        else if (choice == 'C' || 'c')
        {
                cout << "How many hours did you use? ";
                cin  >> numberofHoursUsed;
                baseHours = 0;
                baseFee = 19.95;
                overchargeRate = 0;
                overchargeTime = numberofHoursUsed - baseHours;
                excessFee = overchargeRate * overchargeTime;
                totalFee = baseFee + excessFee;
                cout << "Your charges are $ " << totalFee << endl;
        }
        else if (choice > 'C' || numberofHoursUsed >744)
        {       cout << "You must choose packages A, B, or C. Also, your hours\n";
                cout << "must not exceed 744.\n";
        }
        getch();
 return 0;
}

How do I allow it to :
print the last elseif statement when a user types anything other than A, B, or C or put in more than 744 user hours? I tried that, and this is what happened:
Output Line:
Internet Service Provider
Subscription Packages
A: For $9.95 per month, can get 10 hours of
access. Additional hours are $2.00 per hour.
B: For $14.95 per month, you can get 20 hours
of access. Additonal hours are $1.00 per hour.
C: For $19.95 per month unlimited access is provided.

Please enter your name. Nadia McIntosh
Which subscription package would you like? D
How many hours did you use? 23
Your charges are $ 35.95

See?? Even though I used something that was not part of the menu, (D, for example), it still let me go on with the program.
Here is another problem that I ran into also:
Output Line:
Internet Service Provider
Subscription Packages
A: For $9.95 per month, can get 10 hours of
access. Additional hours are $2.00 per hour.
B: For $14.95 per month, you can get 20 hours
of access. Additonal hours are $1.00 per hour.
C: For $19.95 per month unlimited access is provided.
Please enter your name. Nadia McIntosh
Which subscription package would you like? A
How many hours did you use? 5
Your charges are $ -0.05:sad:
You see the problem? I wanted the price it to stay if it was less than or equal to 10 hours if I choose A, less than or equal to 20 hours if I chose B, and stay the same if I chose C.

Okay, I got that problem solved, but I ran into another problem:

#include <iostream>
#include <iomanip>      //Needed for the showpoint and setprecison command
#include <fstream>      //Needed to use files
#include <string>
#include <conio>        //Needed to show black output screen
using namespace std;
int main()
{
        char choice;
        int numberofHoursUsed, baseHours, overchargeTime;
        string name;
        double excessFee, baseFee, overchargeRate, totalFee;
 // Displays the menu choices  on the screen.
        cout << "\t\tInternet Service Provider\n";
        cout << "\t\tSubscription Packages\n\n";
 cout << "A: For $9.95 per month, can get 10 hours of\n";
        cout << "   access. Additional hours are $2.00 per hour. \n\n" ;
        cout << "B: For $14.95 per month, you can get 20 hours\n";
        cout << "   of access. Additonal hours are $1.00 per hour. \n\n";
        cout << "C: For $19.95 per month unlimited access is provided.\n\n";
        cout << "Please enter your name. ";
        getline (cin, name);
        cout << "Which subscription package would you like? ";
        cin.get(choice);
 
        cout << fixed <<showpoint <<setprecision(2);
 
        if (choice == 'A' || 'a')
        {
                cout << "How many hours did you use? ";
                cin  >> numberofHoursUsed;
                baseHours = 10;
                baseFee = 9.95;
                overchargeRate = 2.00;
                overchargeTime = numberofHoursUsed - baseHours;
                excessFee = overchargeRate * overchargeTime;
                totalFee = baseFee + excessFee;
                cout << "Your charges are $ " << totalFee << endl;
        }
        else if (choice == 'B' || 'b')
        {
                cout << "How many hours did you use? ";
                cin  >> numberofHoursUsed;
                baseHours = 20;
                baseFee = 14.95;
                overchargeRate = 1.00;
                overchargeTime = numberofHoursUsed - baseHours;
                excessFee = overchargeRate * overchargeTime;
                totalFee = baseFee + excessFee;
                cout << "Your charges are $ " << totalFee << endl;
        }
        else if (choice == 'C' || 'c')
        {
                cout << "How many hours did you use? ";
                cin  >> numberofHoursUsed;
                baseHours = 0;
                baseFee = 19.95;
                overchargeRate = 0;
                overchargeTime = numberofHoursUsed - baseHours;
                excessFee = overchargeRate * overchargeTime;
                totalFee = baseFee + excessFee;
                cout << "Your charges are $ " << totalFee << endl;
        }
        else if (choice > 'C' || numberofHoursUsed >744)
        {       cout << "You must choose packages A, B, or C. Also, your hours\n";
                cout << "must not exceed 744.\n";
        }
        getch();
 return 0;
}

How do I allow it to :
print the last elseif statement when a user types anything other than A, B, or C or put in more than 744 user hours? I tried that, and this is what happened:
Output Line:
Internet Service Provider
Subscription Packages
A: For $9.95 per month, can get 10 hours of
access. Additional hours are $2.00 per hour.
B: For $14.95 per month, you can get 20 hours
of access. Additonal hours are $1.00 per hour.
C: For $19.95 per month unlimited access is provided.

Please enter your name. Nadia McIntosh
Which subscription package would you like? D
How many hours did you use? 23
Your charges are $ 35.95

See?? Even though I used something that was not part of the menu, (D, for example), it still let me go on with the program.
Here is another problem that I ran into also:
Output Line:
Internet Service Provider
Subscription Packages
A: For $9.95 per month, can get 10 hours of
access. Additional hours are $2.00 per hour.
B: For $14.95 per month, you can get 20 hours
of access. Additonal hours are $1.00 per hour.
C: For $19.95 per month unlimited access is provided.
Please enter your name. Nadia McIntosh
Which subscription package would you like? A
How many hours did you use? 5
Your charges are $ -0.05:sad:
You see the problem? I wanted the price it to stay if it was less than or equal to 10 hours if I choose A, less than or equal to 20 hours if I chose B, and stay the same if I chose C.

That's okay, I got it. I had to use an if/else statement within the other if/else-if statements. :p

That's okay, I got it. I had to use an if/else statement within the other if/else-if statements. :p

Actually, no you didn't if you did not change your IF statements. else if (choice == 'C' || 'c') is not a proper comparison. What this does is OR the 'C' and 'c' together giving 'c', then test if choice is == to 'c'.

To test an OR condition you need to test each case separately: else if ((choice == 'C') || (choice == 'c')) Also, else if (choice > 'C' || numberofHoursUsed >744) is not a good comparison either. What if someone types in '1' as a choice? It could happen. When you get to this else, you know the choice entered is not A, B, nor C. And since the input for numberofHoursUsed is only in the previous else conditions, you never have a valid numberofHoursUsed at this point. And where you actually do enter it, 800 is a valid value!

Okay, so then what should I do??

Use loops and flags to validate input one input at a time.

//make sure choice is valid and assign
//appropriate values 
//to variables when it is

bool needChoice = true;

while(needChoice)
{
        if (choice == 'A' || choice == 'a')
        {
                baseHours = 10;
                baseFee = 9.95;
                overchargeRate = 2.00; 
                needChoice = false;              
        }
        else if (choice == 'B' || choice == 'b')
        {
                baseHours = 20;
                baseFee = 14.95;
                overchargeRate = 1.00;
                needChoice = false;
        }
        else if (choice == 'C' || choice == 'c')
        {
                baseHours = 0;
                baseFee = 19.95;
                overchargeRate = 0;
                needChoice = false;
        }
        else
        {       
                cout << "You must choose package A, B, or C" << '\n';
        }
}

//obtain and make sure numberofHoursUsed is valid
bool invalid = true;
while(invalid)
{
  cout << "How many hours did you use? ";
  cin  >> numberofHoursUsed;  
  if(numberofHoursUsed < 0 || numberofHoursUsed > 744)
     cout << "invalid input, try again" << '\n';
  else
     invalid = false;
}
       
//deterimine value of overchargeTime 
if(choice == 'C' || choice == 'c')
  overchargeTime = 0;
else
{     
  if(numberofHoursUsed <= baseHours)
    overchargeTime = 0;
  else
    overchargeTime = numberofHoursUsed - baseHours;
}

//do calculations
excessFee = overchargeRate * overchargeTime;
totalFee = baseFee + excessFee;

//show result
cout << fixed << showpoint << setprecision(2);
cout << "Your charges are $ " << totalFee << endl;

Okay, thanks, I got it.

Member Avatar for iamthwee

You should remove the <conio> header and replace the getch() with cin.get() or getchar() which is portable. I have told you this before.

conio is not standard.

okay, thanks
I keep forgetting

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.