Looping part of code with if statements

I'm new here, I've been stuck on this for a while now hopefully you can help me. So what I need help with is where
1
2
3
if (UserSelection != 'L' && UserSelection != 'S')
	{
		cout << "\nInvalid entry, please try again.\n" << endl;

is, I need the program to be able to return back to where you enter either L or S. The program works as long as the correct character is selected. What i need it to do is return to be able to input UserSelection when an invalid entry is entered. I know it has something to do with while loop but I haven't been able to make it work. Your help would be greatly appreciated.
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

#include<iostream>
using namespace std;

void main ()
{
	char UserSelection;
	
    cout << "Are you a salesperson or a loan broker? \n\nPlease enter either 'S' for salesperson or 'L' for loanbroker. ";
	cin >> UserSelection;

	if (UserSelection=='S')
	{
		cout << "\nYou have selected salesperson.\n";

		double carprice;
		cout << "\nEnter the total price of the automobile sale: ";
		cin >> carprice;
		cout << "\n";

		
		if (carprice<=15000)
		{
			cout << "Your total sales commission is: " << carprice*.03 << endl << endl;
			cout << "\n";
		}
		
		
		if (carprice>=15000.01 && carprice<=25000)
		{
			cout << "Your total sales commission is: " << carprice*.04 << endl << endl;
			cout << "\n";
		}
		
		
		if (carprice>25000.01)
		{
			cout << "Your total sales commission is: " << carprice*.05 << endl << endl;
			cout << "\n";
		}
	}

	if (UserSelection=='L')
	{
		cout << "\nYou have selected loan broker.\n";

		double carprice;
		cout << "\nEnter the total price of the automobile sale: ";
		cin >> carprice;
		cout << "\n";

		
		if (carprice<=15000)
		{
			cout << "Your total sales commission is: " << carprice*.005 << endl << endl;
			cout << "\n";
		}
		
		
		if (carprice>=15000.01 && carprice<=25000)
		{
			cout << "Your total sales commission is: " << carprice*.008 << endl << endl;
			cout << "\n";
		}
		
		
		if (carprice>25000.01)
		{
			cout << "Your total sales commission is: " << carprice*.01 << endl << endl;
			cout << "\n";
		}
	}
	
	if (UserSelection != 'L' && UserSelection != 'S')
	{
		cout << "\nInvalid entry, please try again.\n" << endl;
	}

return;
}
1
2
3
4
5
6
while ( UserSelection != 'L' || userSelection != 'S' )
{

Your code here

}


Using multiple if statements without if else and else can be confusing to read. Be sure to use if, else if, and else statements.
Last edited on
Hi, Try using a switch statement with a default case.

Also I have done posts on using a bool controlled while loop with a switch. Use the search function at the top of this page.

Hope all goes well.
I'm supposed to use if, else statements can't use a switch.
Use the if else statements like this
if ( statement )
{
}
else if ( statement )
{
}
else if ( statement )
{
}
else
{
}
Thanks for the help. I just have one question. The first reply said to use:
while ( UserSelection != 'L' || userSelection != 'S' )
{

Your code here

}

Why do I have to use || instead of &&? Isn't || the "OR" operator, meaning only one of the conditions have to be true for the statement to be true.
Yes it is
If you're usiing && userinput need to be 'L' and 'S', 2 value at 1 variable without array ? I don't thnk iits possible
Last edited on
I'm using && and it is working. Thank you guys.
Hi,

Just wondering what you thought about using a switch ?

I personally really hate statements like these:

UserSelection != 'L' && UserSelection != 'S'

because they are ugly, error prone & non scalable.

A switch may take more lines of code, but it is far superior IMO.

Also remember that while code may work, it doesn't necessarily mean it is as good as it could be.

Regards
I'm taking a beginners C++ class and the program required us to use if, else if and else statements. We just learned about switches last class, but I do realize that it would have been a lot easier and efficient using them. Thank you by the way.
@MaxDaniels
@LendraDwi
The if statement is correct with && because the conditions are !=.

If the value is other than L or S, you want the condition to be true.
Using || is incorrect. If the value is S, then != L will be true and the invalid entry logic will be executed. Likewise if the value is L, then != S will be true. This results in the invalid entry logic to be executed regardless of what was entered.

Topic archived. No new replies allowed.