Looping error

This code runs, but with my code, it never gets out of the loop, and I do not know why or what I am doing wrong. I tried this with string first, but changed to int and the problem still persists. Thanks for the help in advance!
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
  #include <iostream>
#include <string> 
#include <fstream>
#include <iomanip>
using namespace std;

int main ()
{
	string adminUsername,
		   adminPassword,
		   studentUsername,
		   studentPassword;
	int    choice = 0;
	
cout << "Welcome to the registration. If you are a teacher, please press 1; if you are a student, please press 2.";
cin >> choice;

while (choice != 1 || choice != 2)
{
	cout << "Please make a valid choice. Enter in 1 if you are a teacher, and 2 if you are a student." << endl;
	cin >> choice;
}

if (choice == 1)
{
	cout << "Please register your username.";
	cin >> adminUsername;
	cout << "Please register your password.";
	cin >> adminPassword;
}
if (choice == 2)
{
	cout << "Please register your username.";
	cin >> studentUsername;
	cout << "Please register your password.";
	cin >> studentPassword;
}

return 0;
}

while (choice != 1 || choice != 2)

If choice were 3, this would evaluate to true because choice != 1 was true and choice != 2 was true.
If choice were 2, this would evaluate to true because choice != 1 would be true.
If choice were 1 this would evaluate to true because choice != 2 would be true.

In order for this to evaluate to false, both conditions must evaluate to false and there is no value that is both 1 and 2.

Equivalent to what you've written:
while (choice == 1 && choice == 2)

What you wanted to write:
while (choice != 1 && choice != 2)
or equivalently:
while (!(choice == 1 || choice == 2))
Last edited on
Ohh so I have to write it so both requirements are met? Because reading it would say if choice is not 1 or choice is not 2. That meant if choice was 1 or 2 you skip the loop:/ That's how I interpreted it lol
Topic archived. No new replies allowed.