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!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <iomanip>

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..

1
2
3
4
else
        amountregularDue = 10.00 + (numofMins * 0.20);
        	cout << "Account number: " << accountNumber << endl;
            cout << "Amount due: $" << amountregularDue << endl;


This should look like..
1
2
3
4
5
6
7
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.

1
2
3
4
5
6
7
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.
1
2
3
4
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)
1
2
3
4
5
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.