I have a problem:

Here is my program:

#include <iostream>
#include <iomanip>      
#include <fstream>      
#include <string>       
#include <conio>
using namespace std;
int main()
{
        char choice;
        string name;
        int hours;
        double charges, total;

        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);
        cout << "How many hours did you use for this month? ";
        cin >> hours;
        if (choice == 'A' || 'a')
        {
                if (hours >=10)
                {
                charges = 9.95;
                total = charges + ((hours - 10) * 2.00);
                cout << name  << " your total payment for this month is $" << total <<endl;
                }
                else cout << name  << " your total payment for this month is $" << charges << endl;
        }
        else if (choice == 'B' || 'b')
        {
                charges = 14.95;
                if (hours >=20)
                {
                cout << name << " your total payment for this month is $" << charges << endl;
                }
                else
                cout <<name << "your total payment for this month is $" << charges << endl;
                total = charges + ( (hours - 20) * 1.00) ;
        }
        else if (choice == 'C' || 'c')
        {
            charges  = 19.95;
            cout <<name << " your total payment for this month is $" << charges << endl;
        }
        else if (choice !='C' || hours != 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:
When I run the program and choose C for my choice, it should output the name of the subscriber, and your total payment for this month is $19.95 (since it's unlimited). But, this is what happens:

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? C
How many hours did you use for this month? 35
Nadia McIntosh your total payment for this month is $59.95

Also, everything in the first subscription choice (which is A) calculates fine and everything. But, watch what happens when I choose B:

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? B
How many hours did you use for this month? 25
Nadia McIntosh your total payment for this month is $39.95

See what happens..My calculations are all wrong.
What's wrong??:X

Recommended Answers

All 7 Replies

Since 'a' is always true, the program never gets past this statement.

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

Have to expand it to:

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

Same with all the other if statements.

Some logic errors:

else if (choice == 'B' || choice == 'b')
        {
                charges = 14.95;
                if (hours >=20)
                {
                // ??? nothing here
                cout << name << " your total payment for this month is $" <<
                charges << endl;
                }
                else
                cout <<name << "your total payment for this month is $" << 
                charges << endl;
                // how about putting this up in the if statement?
                total = charges + ( (hours - 20) * 1.00) ;
        }

Okay, I got Subscription A and C to work correctly (Thanks!). But, for some reason, it's still allowing me to enter hours higher than 744 and my calculations for Subscription B is still not kicking in.

Here is my code again. Run it, and you will see what I mean.

#include <iostream>
#include <iomanip>      //Needed for the showpoint and setprecison command
#include <fstream>      //Needed to use files
#include <string>       //Needed to use string variable
#include <conio>
using namespace std;
int main()
{
        char choice;
        string name;
        int hours;
        double charges, total;
 // 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' || choice =='a')
        {
                cout << "How many hours did you use for this month? ";
                cin >> hours;
                if (hours >=10)
                {
                charges = 9.95;
                total = charges + ((hours - 10) * 2.00);
                cout << "Your total payment for this month is $" << total <<endl;
                }
                else cout <<name << " , your total payment for this month is $" << charges << endl;
        }
        else if (choice == 'B' || choice == 'b')
        {
                cout << "How many hours did you use for this month? ";
                cin >> hours;
                charges = 14.95;
                if (hours >=20)
                {
                total = charges + ( (hours - 20) * 1.00) ;
                cout << name << ", your total payment for this month is $" << charges << endl;
                }
                else
                cout << "Your total payment for this month is $" << charges << endl;

        }
        else if (choice == 'C' || choice == 'c')
        {
            charges  = 19.95;
            cout << name << ", your total payment for this month is $" << charges << endl;
        }
        else if (choice !='C' || hours >744)
        {       cout << "You must choose packages A, B, or C. Also, your hours\n";
                cout << "must not exceed 744.\n";
        }
        getch();
        return 0;
}

You're printing out the wrong thing for 'b'. Try 'total' instead of 'charges' and you will see what I mean.

Regarding the hours, you have several choices. Either place the "enter number of hours" statement beforet the series of if()s, so that the first thing you can check is whether the user's input is valid. The only problem with this solution would be that it would ask the user for the number of hours in plan 'C'.

The other solution is to check the user input each time the user enters something. You might want to make a separate function for validating the user input if you choose this solution.

Can you show me in the program where I would put it please?

>Can you show me in the program where I would put it please?
I don't even know what solution you're trying to do.

Here's some pseudocode to illustrate solution 1 better.

...
get number of hours

if number of hours is invalid then
   exit
end if

else if menu choice is 'A' then
   blah blah blah
end if

etc...

else
    menu choice is invalid

The solution that I am trying to achieve is this: I am trying to get my calculations to print correctly. When you run my program and test it, you will see what I mean.

I figured it out. It was some simple mistakes. Thanks.

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.