### I'm so close! Help!

I'm stuck on just my last bit of code with this problem, its a basic cell phone bill program that computes your bill depending on what type of service you have, how many minutes you've used, and what time of day you used them in the case of premium service. The "Regular Service" section is working just fine but I'm having an issue in the if and else section in the "Premium service" section, it's spitting back the wrong answer and it looks like it's using the formulas from the regular service section for some reason. Any and all help would be greatly appreciated and I need to have this figured out ASAP!
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798`` ``````#include #include using namespace std; int main() { //Variables int accountNumber; char serviceType; int numofMins; int numofminsDay; int numofminsNight; double amountregularDue; double amountpremiumdayDue; double amountpremiumnightDue; cout << fixed << showpoint; cout << setprecision(2); cout << "This program computes a cell phone bill." << endl; cout << "Enter account number (an integer): "; cin >> accountNumber; cout << endl; cout << "Enter service type: " << "R or r (Regular Service), " << "P or p (Premium Service): "; cin >> serviceType; cout << endl; switch (serviceType) { case 'r': case 'R': cout << "Enter the number of minutes used: "; cin >> numofMins; cout << endl; if (numofMins <= 50) { amountregularDue = 10.00; cout << "Account number: " << accountNumber << endl; cout << "Amount due: \$" << amountregularDue << endl; } else amountregularDue = 10.00 + (numofMins * 0.20); cout << "Account number: " << accountNumber << endl; cout << "Amount due: \$" << amountregularDue << endl; break; case 'p': case 'P': cout << "Enter the number of minutes used during the day (6AM - 6PM): "; cin >> numofminsDay; cout << endl; cout << "Enter the number of minutes used during the night (6PM - 6AM): "; cin >> numofminsDay; cout << endl; if (numofminsDay <= 75) { amountpremiumdayDue = 25.00; cout << "Account number: " << accountNumber << endl; cout << "Amount due for day usage: \$" << amountpremiumdayDue << endl; } else amountpremiumdayDue = 25.00 + (numofminsDay * 0.10); cout << "Account number: " << accountNumber << endl; cout << "Amount due for day usage: \$" << amountpremiumdayDue << endl; if (numofminsNight <= 100) { amountpremiumnightDue = 25.00; cout << "Account number: " << accountNumber << endl; cout << "Amount due for night usage: \$" << amountpremiumnightDue << endl; } else if (numofminsNight > 100) { amountpremiumnightDue = 25.00 + (numofminsDay * 0.05); cout << "Account number: " << accountNumber << endl; cout << "Amount due for night usage: \$" << amountpremiumnightDue << endl; } else cout << "You did this incorrectly."; break; default: cout << "Invalid customer type." << endl; } return 0; }``````
@rar0012

When you have an else, you should encase everything you want printed or computed, inside of curly brackets. Such as your code here, and others like it..

 ``1234`` ``````else amountregularDue = 10.00 + (numofMins * 0.20); cout << "Account number: " << accountNumber << endl; cout << "Amount due: \$" << amountregularDue << endl;``````

This should look like..
 ``1234567`` ``````else { amountregularDue = 10.00 + (numofMins * 0.20); cout << "Account number: " << accountNumber << endl; cout << "Amount due: \$" << amountregularDue << endl; } break;``````

This should get you on the right track..
Gotcha, I've been confused about this because in my text book there are several examples that don't include the {} in the else's for some reason. But it seems like I'm still having issues with the second section even when I've closed off all the else parts in both :[
@rar0012

Here in this code, you have a problem.

 ``1234567`` ``````cout << "Enter the number of minutes used during the day (6AM - 6PM): "; cin >> numofminsDay; cout << endl; cout << "Enter the number of minutes used during the night (6PM - 6AM): "; cin >> numofminsDay; cout << endl;``````

You're using the same variable for both. You should name each one differently, then use numofminsDay to add both of them together, to get the full minutes used.
 I've been confused about this because in my text book there are several examples that don't include the {} in the else's for some reason.

http://www.cplusplus.com/doc/tutorial/control/

When you do not have the braces, only the next instruction is part of the if statement.
 ``1234`` ``````if(this_is_true) cout << "Yay, I am true."; //Part of if statement ++var; //This line is outside the statement do_something(); //This too ``````

To have a multi-line if body, use the curly braces (or use them all the time)
 ``12345`` ``````if(this_is_true){ cout << "Yay, I am true."; //Part of if statement ++var; //Now this line is also part of the if statement } do_something(); //This is still outside ``````

This also goes for `else if`, `else`, and other structures such as loops.
Last edited on
Wow I had a feeling it would be something simple like that, thanks a bunch!
Topic archived. No new replies allowed.