Issue with an if else if program

Having an issue with the correct execution of this program

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
 #include <iostream>
#include <iomanip>
using namespace std;

int main()

{
		double minutes
		(minutes >= 1.00)
		int choice, A, B, C;
		A = 1;
		B = 2;
		C = 3;
		const double costA = 19.95, costB = 32.95, costC = 45.95;
		double costTotal;

		cout << "Which Package have you purchased?" << endl;
		cout << "Package: A, B, or C?" << endl;	
		cin >> choice;

		cout << setprecision(2) << fixed;

		if (choice = A)
		{
			cout << "How many minutes spent on the phone during this month?" << endl;
			double  minutes;
			cin >> minutes;
			costTotal = costA + (minutes - 200) * 0.08;
			cout << "Your monthly bill is: " << "$" << costTotal << endl;
		}

		else if (choice = B)
		{
			cout << "How many minutes spent on the phone during this month?" << endl;
			double  minutes;
			cin >> minutes;
			costTotal = costB + (minutes - 600) * 0.05;
			cout << "Your monthly bill is: " << "$" << costTotal << endl;
		}

		else if (choice = C)
		{
			costTotal = costC;
			cout << "Your monthly bill is: " << "$" << costTotal << endl;
		}


return 0;
}


i also need to add a single loop in case someone makes an incorrect input but i'm not worried about it at the moment. My main concern is that i'm getting

if i remove the make the minutes = 1 then it works but i need the program to understand minutes needs to be greater than or equal to 1, and it still won't allow input for any of the if statements

1
2
3
4
5
6
7
Which Package have you purchased?
Package: A, B, or C?
A
How many minutes spent on the phone during this month?
Your monthly bill is: $-7404770507945427000000000000000000000000000000000000000
00000.00
Press any key to continue . . .
Last edited on
closed account (48T7M4Gy)
1
2
3
4
if (minutes > etc)
{
   code ...
}
= is used for assignment while == is used to compare two things. Make sure you use the correct one.
closed account (48T7M4Gy)
You should also initialize costTotal at line 15
closed account (48T7M4Gy)
Try using a switch control instead of all the repetitive coding?
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{
		double minutes;
		minutes >= 1.00;
		int choice, A, B, C;
		A = 1;
		B = 2;
		C = 3;
		const double costA = 19.95, costB = 32.95, costC = 45.95;
		double costTotal;

		cout << "Which Package have you purchased?" << endl;
		cout << "Package: A, B, or C?" << endl;	
		cin >> choice;

		cout << setprecision(2) << fixed;

		if (choice == A)
		{
			cout << "How many minutes spent on the phone during this month?" << endl;
			double  minutes;
			cin >> minutes;
			costTotal = costA + (minutes - 200) * 0.08;
			cout << "Your monthly bill is: " << "$" << costTotal << endl;
		}

		else if (choice == B)
		{
			cout << "How many minutes spent on the phone during this month?" << endl;
			double  minutes;
			cin >> minutes;
			costTotal = costB + (minutes - 600) * 0.05;
			cout << "Your monthly bill is: " << "$" << costTotal << endl;
		}

		else if (choice == C)
		{
			costTotal = costC;
			cout << "Your monthly bill is: " << "$" << costTotal << endl;
		}


return 0;
}

returns with
1
2
3
4
Which Package have you purchased?
Package: A, B, or C?
A
Press any key to continue . . .


nothing is happening

also my instructor doesn't want us using break so i saw no point in using switch, and costTotal won't have a value until later on in the if statements
Last edited on
A is an integer variable in your program holding the value 1 so for the choice variable to be equal to A you need to input 1.

If you want to enter letters into your program you probably want to change choice into a char and compare it to the character literal 'A' instead.
Last edited on
closed account (48T7M4Gy)
1. Unless he means you are limited to not using a break outside a switch control. Even then that sort of limitation is dubious.

2. If you are repeating lines like 25 to 27 then the code can be improved, simplified and debugged more easily.

3. Read the comments I made carefully. Your line 9 is doing nothing and I have already described how you solve the if minutes problem. Take it or leave it I guess. We are talking about nested if's - nothing special.

4. Initializing is not mandatory but it is a warning a good compiler will give you which is well worth taking head of and really at no cost.
Peter, let me change A, B, C to character literals, and see if i'm still having issues

but how would i go about assuring that the program knows minutes has to be greater than or equal to 1
Last edited on
closed account (48T7M4Gy)
It's also choice == 'A' etc
Last edited on
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{
		double minutes;
		char ch;
		ch = 'A';
		ch = 'B';
		ch = 'C';
		int choice;
		choice = 'A', 'B', 'C';

		const double costA = 19.95, costB = 32.95, costC = 45.95;
		double costTotal;

		cout << "Which Package have you purchased?" << endl;
		cout << "Package: A, B, or C?" << endl;	
		cin >> choice;

		cout << setprecision(2) << fixed;

		if (choice == 'A')
		{
			cout << "How many minutes spent on the phone during this month?" << endl;
			cin >> minutes;
			costTotal = costA + (minutes - 200) * 0.08;
			cout << "Your monthly bill is: " << "$" << costTotal << endl;
		}

		else if (choice == 'B')
		{
			cout << "How many minutes spent on the phone during this month?" << endl;
			cin >> minutes;
			costTotal = costB + (minutes - 600) * 0.05;
			cout << "Your monthly bill is: " << "$" << costTotal << endl;
		}

		else if (choice == 'C')
		{
			costTotal = costC;
			cout << "Your monthly bill is: " << "$" << costTotal << endl;
		}


return 0;
}


still getting this:
1
2
3
4
5
6
7
Which Package have you purchased?
Package: A, B, or C?
A
How many minutes spent on the phone during this month?
Your monthly bill is: $-740477050794542700000000000000000000000000000000000000
00000.00
Press any key to continue . . .


it seems there is is an also overflow and i'm not sure what's causing it?
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    double minutes;
    char choice = '\0';
    
    const double costA = 19.95, costB = 32.95, costC = 45.95;
    double costTotal = 0;
    
    cout << "Which Package have you purchased? " << endl;
    cout << "Package: A, B, or C? " << endl;	
    cin >> choice;

    cout << setprecision(2) << fixed;

    if ( choice == 'A' or choice == 'B' )
    {
        cout << "How many minutes spent on the phone during this month? " << endl;
        cin >> minutes;
		
        if (choice == 'A')
	        costTotal = costA + (minutes - 200) * 0.08;
	    
         if (choice == 'B')
	        costTotal = costB + (minutes - 600) * 0.05;
    }

    if (choice == 'C')
        costTotal = costC;
	    
    cout << "Your monthly bill is: " << "$" << costTotal << endl;


    return 0;
}
Last edited on
now i get what you were saying just changing it to simplify and therefore removing any syntax errors, i was really losing my mind.

is there a way I can add a single loop to this code in case an incorrect character is used such as D
closed account (48T7M4Gy)
Use a while loop that 'wraps' around everything and have a new additional option 'Q' for quit.

This is where intialization comes in. If you don't initialize properly then the trap is you never get into the loop.

while choice is not quit or a,b or c
{
do stuff
loop back
}

http://www.cplusplus.com/doc/tutorial/control/
Last edited on
I'm reading through it right now
i attempted
 
 while (choice != 'A' || 'B' || 'C')


but it's triggered by the input for minutes
Last edited on
closed account (48T7M4Gy)
1. The loop must wrap around all of the stuff you have so far.
2. ... and the 'door' to the loop will only open while ( choice == 'A' or choice == 'B' or choice == 'C' or choice != 'Q' )
3. You might have to bracket choices eg ( choice == 'A) or (choice == ...) etc. It is better if you do anyway for clarity in you while statement.
closed account (48T7M4Gy)
Here's another way of doing it with bool:
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    double minutes;
    char choice = '\0';
    
    bool keepGoing = true;
    
    const double costA = 19.95, costB = 32.95, costC = 45.95;
    double costTotal = 0;
    
    cout << setprecision(2) << fixed;
    
    while ( keepGoing == true )
    {
        cout << "Which Package have you purchased? " << endl;
        cout << "Package: A, B, or C or Q to quit? " << endl;	
        cin >> choice;
        
        if ( choice == 'A' or choice == 'B' )
        {
            cout << "How many minutes spent on the phone during this month? " << endl;
            cin >> minutes;
		
            if (choice == 'A')
	            costTotal = costA + (minutes - 200) * 0.08;
	    
            if (choice == 'B')
	            costTotal = costB + (minutes - 600) * 0.05;
        }

        if (choice == 'C')
            costTotal = costC;
            
        if (choice == 'Q' )
            keepGoing = false;
        else
            cout << "Your monthly bill is: " << "$" << costTotal << endl;
        
    }
    return 0;
}
Topic archived. No new replies allowed.