If-else statements outputting all answers

Hi hello, I'm new, I'm sure you've seen this all before. I've done lots of searching for my problem and looking at samples of code, but I can't quite figure out what's going on with mine or what the name of the issue is so I can search for solutions easier.

I have seen solutions to issue using switch statements, however this is for class and lab is requesting if-else statements, so I think I have to stick to those.
I posted entire code but I believe it's the if-else section that's causing problem:

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

#include <iostream>
#include <cmath>

using namespace std ;

int main()


{
double totalBill , PlanA , PlanB , PlanC ;

PlanA = 39.99 ;
PlanB = 59.99 ;


bool minute ;


 	cout.setf(ios::fixed) ;
 	cout.setf(ios::showpoint) ;
 	cout.precision(2) ;

char plan ;



cout << " Rate Plans: \n" << " Package A: 450 minutes, $39.99.\n" ;
cout << " Package B: 900 minutes, $59.99. \n " ;
cout << " Package C: Unlimited minutes for $69.99.\n " << endl;

cout << " Enter in your package letter (A, B, or C) " << endl;

cin >> plan ;

cout << " Enter in number of minutes used during this month: \n "  << endl;

cin >> minute ;

if ( plan == 'A' || 'a' ) ;
{
    if ( minute <= 450 ) 
    
	    
	    	cout << "Bill amount is $ " << PlanA << endl ;
    		
	 else if (( minute > 450 ) ) ;
		
			totalBill = ( 39.99 + .45 * ( minute - 450 ) ) ;
			cout << "Bill is $ " << totalBill << endl ;
		
}
if ( plan == 'B' || 'b' ) ;
{
    if ( minute <= 900 ) {
	    
	    	cout << "Bill amount is $ "<< PlanB << endl ;
    }		
	 else if (( minute > 900 ) ) ;
		{
			totalBill = ( 59.99 + .45 * ( minute - 450 ) ) ;
			cout << "Bill is $ " << totalBill << endl ;
		}
}       
if ( plan == 'C' || 'c' ) ;
{
	    
	    	cout << "Bill amount is $69.99 " << endl ;
   
}       
	return  0 ;
}


My output, regardless of what's entered, reveals all answers and computes all math functions inside statements. Looks like this:

Output:

Rate Plans:
Package A: 450 minutes, $39.99.
Package B: 900 minutes, $59.99.
Package C: Unlimited minutes for $69.99.

Enter in your package letter (A, B, or C)
a
Enter in number of minutes used during this month:

30
Bill amount is $ 39.99
Bill is $ -162.06
Bill amount is $ 59.99
Bill is $ -142.06
Bill amount is $69.99
Last edited on
First off, instead of using just "if"s, you should be using
1
2
3
4
5
if { ... }
else if { .. }
else if { ... }
... 
else { ... }
pattern so that 1 and only 1 option is ever revealed.

if ( plan == 'A' || 'a' ) ;
Two major things wrong here.

1. Do not put a semi-colon after an if clause. This makes the if clause useless.
Get rid of every place you have a semi-colon after an if statement.

2. Each conditional separated by || or && needs to be its own, independent statement.
Change to:
1
2
3
4
if ( plan == 'A' || plan == 'a' )
{
    ...
}

Change this in every place you have in in your code, as well.

Last edited on
Thanks so much for the help. I really appreciate it. The semi-colons were a goofy fix to a problem I wasn't understanding. I went and made the changes and followed the pattern you gave, and it helped a lot.

However, I would like to know how to format each if-else statement, because I changed it to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

if  (( plan == 'A' || plan == 'a' ) && (  minute > 450))
    
    { 
        totalBill = ( 39.99 + .45 * ( minute - 450 ) ) ;
			cout << "Bill is $ " << totalBill << endl ; 
	}
   
    else if  ( minute <= 450 )
	    {       
			cout << "Bill is $ " << PlanA << endl ;
	    }

    else
    { cout << " Please enter A, B, or C and your used minutes. " << endl ; }


I'm sorry about the braces being all over the place. I keep switching things around. When I run the code, I get PlanA answer (39.99), regardless amount of minutes.

bool minute ;
You are comparing minute against numbers. A bool can only be true or false. Change minute to be an int or a double.

_________________________________

In adding that && to your if statement, you changed the logic flow of your code.
If your plan is anything but A/a, and you have less than 450 minutes, you will enter the else if statement. If you chose plan == 'C' and minute == 500, you would reach your else statement.

Make sure you are able to describe to yourself the plan in words first instead of code, and then try to translate the words into code, thinking about what each possible choice/outcome will be.

It's okay to have ifs within ifs when necessary, I didn't mean to imply that part of your structure was wrong. I was referring to your lines 40, 53, and 65 when saying "if, else if" structure would be preferred there (A switch would be the best to use here, but you said you didn't want that).

____________________

You also have a lot of code that's difficult to maintain. You define PlanA and PlanB as some numbers, but then don't initialize PlanC. You define PlanA and PlanB as 39.99 and 59.99, but then you use the raw numbers (lines 49 and 61 in your OP). This is commonly known as a "magic number" in programming, and I'd suggest you replace it with a named variable. It also makes it more maintainable because you only need to change one number instead of a bunch of instances of the same number.

https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants

You also have a lot of repetition of code, such as on lines 49 and 50, and 61 and 62. It would be useful to factor this into a function and pass in which plan you want, but if this is above what you've learned so far, don't worry about that.


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
PlanC = 69.99;
const int PlanA_minutes = 450;
const int PlanB_minutes = 900;

totalBill = 0.00; // default if not on a plan
if (plan == 'A' || plan == 'a')
{
    if (minute <= PlanA_minutes ) 
    {
        totalBill = PlanA;
    }
    else // "else if" is not necessary here since there's only 1 other possibility
    {
        totalBill = ( PlanA + .45 * ( minute - PlanA_minutes ) );
    }	
}
else if (plan == 'B' || plan == 'b') 
{
    if (minute <= PlanB_minutes )
    {	    
        totalBill = PlanB;
    }
    else
    {
        totalBill = ( PlanB + .45 * ( minute - PlanB_minutes ) ); // changed from minute - 450, not sure if this was intended
    }
}  
else if ( plan == 'C' || plan == 'c' )
{
    totalBill = PlanC;
}
cout << "Bill amount is $ " << totalBill << endl;


That would be how I would personally format it.
Last edited on
Topic archived. No new replies allowed.