Need help on if else problem.

Write a program that calculates a customer’s monthly bill. It should ask which package the customer has purchased and how many hours were used. It should then display the monthly charge, additional hours charge and total amount due.

Package A: For $ 9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour.

Package B: For $ 14.95 per month 20 hours of access are provided. Additional hours are $ 1.00 per hour.

Package C: For $ 19.95 per month unlimited access is provided.



# include <iostream>
# include <iomanip>
using namespace std;

int main()
{
double total = 0, charge = 0;//Stores total amount paid and initial charge
char choice ;//Stores input for package choice
double chargeA = 9.95;
double chargeB = 14.95;
double chargeC = 19.95;
int hours = 0;//Stores hours used

//Displays package choices to user
cout << "Package A: For $ 9.95 per month 10 hours of access are provided" << endl;
cout << "Additional hours are $2.00 per hour." << endl << endl;

cout << "Package B: For $ 14.95 per month 20 hours of access are provided."<< endl;
cout << "Additional hours are $ 1.00 per hour." << endl << endl;

cout << "Package C: For $ 19.95 per month unlimited access is provided." << endl << endl;

cout << "Please type in letter A, B, or C for your package choice." << endl << endl;

cin >> choice; //Stores user input for package choice

cout << "Now please enter how many hours was used." << endl << endl;

cin >> hours; //Stores user input for amount of hours used

//Calculates totals depending on which package user chose and hours entered




if (choice == 'A' || choice == 'a')
{
if (hours >=10)

total = ((hours - 10) * 2) + chargeA;
cout << "Your payment for this month is $" << setprecision(2) << fixed << total << endl << endl;
}


else if (choice == 'A' || choice == 'a')
{
if (hours <= 9 && hours >= 0)
total = chargeA;
cout << "Your payment for this month is $" << setprecision(2) << fixed << total << endl << endl;
}


return 0;

}





So far I have only worked on package A part but when I run the program, if I type any hours 10 or above the program works fine and runs the right commands, but once I get to the part where I entered an amount of hours less than 10, it just comes out to the answer 0 as if like it just skipped the whole line of code and did not run the line where it begins "else if".



Anyone know what I am doing wrong?
Yeah, think about how your if-else works.

The first if will be hit if the input is A/a. That means the else can never be hit.

A better approach would be...
1
2
3
4
5
6
7
8
9
10
11
if( choice == 'A' || choice == 'a' )
{
   if( hours > 0 && hours < 10 )
   {
      total = chargeA;
   }
   else
   {
      total = ( hours - 10 ) * 2 + chargeA;
   }
}
When I changed it to that, now the first part of the code works where if I enter any number between 0 and 10 it works correctly, but if I enter any number greater than 10 it does not display the result.
Probably something to do with wherever you've put your print statement.

Given that you're probably going to want it to print no matter what choice is input, put it after your if statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
if( choice == 'A' || choice == 'a' )
{
   if( hours > 0 && hours < 10 )
   {
      total = chargeA;
   }
   else
   {
      total = ( hours - 10 ) * 2 + chargeA;
   }
}

// Print total here 
now when I do that it displays both of my cout statements at the same time and any number over 10 wont work still, wierd
Show me your current code.

Make sure you enclose it in code tags (use the <> button).
if (choice == 'A' || choice == 'a')
{
if (hours >0 && hours <10)

total = chargeA;
}
cout << "Your payment for this month is $" << setprecision(2) << fixed << total << endl << endl;

else
{
total = (hours - 10) * 2 + chargeA;

}
cout << "Your payment for this month is $" << setprecision(2) << fixed << total << endl << endl;
sorry im new to the site I pressed the button but not sure what it did or how I was supposed to
i figured it out, had to use another if statement rather than else and it seems to be working both ways now, thanks for the help tho!
Topic archived. No new replies allowed.