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!
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
usingnamespace 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;
}
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))
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