I recently started to get into c++, and i have a (probably) very simple question. I am supposed to write a password program, so far so good. Then i wanted to make the program work for multiple users, in this case
martin
monika
joerg
What i want the program to do is give out the message "invalid user" when another username is entered, and proceed to the password part of the program when one of those usernames is entered.
However (at this stage) the program always couts "Invalid user" even when the entered username is one of those three. Where am I thinking wrong?
Thanks in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
string username;
cout << "Please Enter your User Name: ";
cin >> username;
if ( ( username != "martin") || (username != "monika") || (username != "joerg"))
{
cout << "Invalid User, please try again" << endl;
continue;
}
compare for strings is getting probably to advanced for him and really it isn't needed for this.
As tath said it will always be true for you if condition.
Here is why. The || operator is the OR operator. Basically it is always true because of this. Lets say you enter the name "martin", when you enter the if condition this is what is happening.
( username != "martin") is FALSE
(username != "monika") is TRUE
(username != "joerg") is TRUE
Since you are using the || operator you condition is saying, "If any one of the condition's are true it returns true. So even though the first one is false the other two are true so the condition returns true. Wow that was confusing even to me lol.
Anyways the operator you are looking for is the && operator instead of the ||. The && operator is the AND operator.
The above code is saying this. If all of them statements in the condition are true, the condition is true. But if any one of the statements are false the condition is false.
Hope that helps and sorry if I confused you a bit ;p
Thank you kind sir. Meanwhile i tried a different approach that worked for me. I realize this is probably crude code, but keep in mind i started one week ago :)
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
//Variablen
string username;
string password;
int x = 0;
while (1)
{
cout << "Welcome to Cyberdyne Systems!" <<endl;
cout << "Please Enter your User Name: ";
cin >> username;
if (username == "martin" || username == "monika" || username == "joerg")
{
while (1)
{
cout << "Enter your password: ";
cin >> password;
if ( (username == "martin" && password == "hund") ||
(username == "monika" && password == "katze") ||
(username == "joerg" && password == "maus")
)
{
cout << "Login succesful";
return 0;
}
elsebreak;
}
}
else
{
cout <<"Invalid user" << endl;
x++;
if (x > 2)
{
cout << "Login failed";
return 0;
}
elsecontinue;
}
}
}
Sadly, i dont understand the difference between what went wrong the first time, and why its working this time. Maybe you can explain why the first code that i posted is faulty, while this one works perfectly.
Thanks in advance
Fauch
edit: Zereos post is what i was looking for. I suspected my use of boolean operators being faulty. Thank you all.