Choose one if-statement

What I want the program to do is to only choose one of the three if/else statement.

If the user inputs A, I want the program to pick the if/else statement that belongs to A, and then stop. I don't want it to show me the results for the B if/else statement, or the C if/else statement.

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
  if (p = 'A' || 'a' && h<=50) 
	{
		cout<< "Your total for this month is: $"<< base_A << endl<<endl;
	}
	else
	{
		cout<< "Your total for this month is: $" << (2*(h-50))+base_A << endl<<endl;
	}

		if (p = 'B' || 'b' && h<=100)
		{
			cout<< "Your total for this month is: $"<< base_B << endl<<endl;
		}
		else
		{
			cout<< "Your total for this month is: $"<< (1.5*(h-100))+base_B << endl<<endl;
		}
	
			if (p = 'C' || 'c' && h<=150)
			{
				cout<< "Your total for this month is: $" << base_C << endl<<endl;
			}
			else
			{
				cout<< "Your total for this month is: $" << (1*(h-150))+base_C << endl<<endl;
			}
You have some problems with your if statement conditions; you need to use == for equality comparison as = does assignment.

Also, conditions like p = 'A' || 'a' && h<=50 don't work the way you think. That has three parts:
p = 'A' (which as stated above is assignment, and should be ==)
'a' (which is always true as the ASCII code for 'a' is non-zero)
h<=50
You need to explicitly write out the part involving 'a' with p == in front.

You could use a switch statement which would tidy it up a little and make it more readable.

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

	// convert to uppercase whatever
	// is in p so it doesn't matter
	// what the user enters :)
	p = toupper(p);

	switch (p)
	{
		case 'A':
			if (h <= 50)
				cout << "Your total for this month is: $" << base_A << endl << endl;
			else
				cout << "Your total for this month is: $" << (2 * (h - 50)) + base_A << endl << endl;
		break;
		case 'B':
			if (h <= 100)
				cout << "Your total for this month is: $" << base_B << endl << endl;
			else
				cout << "Your total for this month is: $" << (2 * (h - 50)) + base_B << endl << endl;
		break;
		case 'C':
			if (h <= 150)
				cout << "Your total for this month is: $" << base_C << endl << endl;
			else
				cout << "Your total for this month is: $" << (2 * (h - 50)) + base_C << endl << endl;
		break;
	}

Zhuge -- That doesn't fix my problem of the program using the if/else statement associated with the letter. It executes all the if/else statements, I only want it to execute the one that goes with the letter.

Softrix -- My teacher wants us to use if/else statements. We're not supposed to know what switch or break mean. Also, the program must recognize upper case 'A', and lower case 'a' as the same letter.
@awkward:

Your best course of action is to try to implement Zhuge's suggestions and then post your modified code again. Then we can help you make further adjustments to your code.
Try this format:
1
2
3
4
5
6
7
8
if (p == 'A' || p == 'a') {
    if (h <= 50)
        cout<< "Your total for this month is: $"<< base_A << endl<<endl;
    else
        cout<< "Your total for this month is: $" << (2*(h-50))+base_A << endl<<endl;
}
else if (...) { ... }
...
Last edited on
Aside from what others pointed out, control flow is not indentation-sensitive. What you wrote is equivalent to (and less readable than)
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
if (p = 'A' || 'a' && h<=50) 
{
	cout<< "Your total for this month is: $"<< base_A << endl<<endl;
}
else
{
	cout<< "Your total for this month is: $" << (2*(h-50))+base_A << endl<<endl;
}

if (p = 'B' || 'b' && h<=100)
{
	cout<< "Your total for this month is: $"<< base_B << endl<<endl;
}
else
{
	cout<< "Your total for this month is: $"<< (1.5*(h-100))+base_B << endl<<endl;
}
	
if (p = 'C' || 'c' && h<=150)
{
	cout<< "Your total for this month is: $" << base_C << endl<<endl;
}
else
{
	cout<< "Your total for this month is: $" << (1*(h-150))+base_C << endl<<endl;
}
Last edited on

@ awkward - sorry, didn't realise, anyway Smac89's post will solve it for you.
Topic archived. No new replies allowed.