2

In the following code I have been trying to get it to check for multiple parameters (when checking if the username AND password are correct).

 string username;
    string password;
    cout << "Hello there. To access this program, please put in your user name and password." << endl;
    cout << "Username: ";
    cin >> username;
    cout << "Password: ";
    cin >> password;
    if(username != "admin", password != "therealpassword"){
        do{
            cout << "Username or password is incorrect." << endl;
            cout << "Username: ";
            cin >> username;
            cout << "Password: ";
            cin >> password;
        }while(username != "admin", password != "therealpassword");
    }

For whatever reason, when running the program and I put in the incorrect username, but the correct password, it allows me to login. At the (do...while) section, if I put in the wrong username, but the right password again, I am able to login correctly still, even though the username is incorrect. How can I get this to check for both the username and password? Any and all help is appreciated.

Theroarx
  • 73
  • 2
  • 9
  • 3
    Possible duplicate of [comma operator in if condition](https://stackoverflow.com/questions/16475032/comma-operator-in-if-condition) – miradulo Apr 14 '18 at 03:56
  • Just to make the behavior of your code more clear: the comma operator `,` is a sequence point. In other words if you write `a, b` in an expression (as you did inside the `if` condition) it evaluates `a`, discards its value, evaluates `b`, and then return its value. This is why your condition is always evaluated to `true` so long as the password is correct. – Garrett Gutierrez Apr 14 '18 at 04:07

3 Answers3

4

The comma does not do what you think it does. You want the logical AND operator && or the logical OR ||. In your case you want the if to be false if both conditions are true. So

Change your if statement to if( !( (username == "admin" ) && (password == "therealpassword" )) )

I would say better is to use string.compare

if( !( (username.compare("admin") == 0) && (password.compare("therealpassword") == 0) ) )

The while statement you want to be true if either are false so it would need to be the logical OR ||.

while( (username.compare("admin") != 0) || (password.compare("therealpassword") != 0) )

A simpler approach would be just use the do while loop and forget the if statement altogether.

Matt
  • 2,554
  • 2
  • 24
  • 45
  • 1
    But that is checking if both statements are true, and if only one or the other is wrong, the login is still a success. I need it to stop the login if only one of them (user or pass) is wrong. – Theroarx Apr 14 '18 at 04:06
  • @Theroarx then you can use `||` operator to get the `OR` condition. – HMD Apr 14 '18 at 04:08
  • @Hamed - that's true, but it still means this answer is incorrect. – Peter Apr 14 '18 at 04:09
  • Ok. Thanks for the clarification. – Theroarx Apr 14 '18 at 04:11
  • @Peter Hmm, I would say it's incomplete and should be updated. – HMD Apr 14 '18 at 04:11
  • 1
    @Hamed interestingly, if this did work, the intuitive implication of `,` would in fact be logical AND – Aluan Haddad Apr 14 '18 at 04:11
  • @AluanHaddad I don't follow, How? – HMD Apr 14 '18 at 04:20
  • @Hamed - if an answer is wrong, it's wrong. The OP is seeking to reject a username/password pair if either is wrong. This answer doesn't achieve that. – Peter Apr 14 '18 at 04:22
  • @Peter I'm not disagree with you but first time I saw the question I think the OP want's `AND` too, because when one see `username` **and** `password` always think of `AND`. – HMD Apr 14 '18 at 04:26
  • The extra `()` in the first example is redundant. Why would it be better to use `compare`? It's less intuitive, harder to read. – super Apr 14 '18 at 04:38
  • @Hamed - the OP was concerned at ability to put in wrong username and right password, and still be allowed to "login". With this answer, if the wrong username OR wrong password are entered the first time, the user will be allowed to "login". But, if correct username and correct password are entered the first time, the user will be told they got it wrong, and prompted for username and password again. – Peter Apr 14 '18 at 04:38
  • @Peter I get that, I didn't give the answer to be punished :D But fortunately the OP asked for clarification in the comments and gave the answer a chance to be updated. I think that's the reason why comment and edit exist. – HMD Apr 14 '18 at 04:42
  • @Hamed by natural language. If x, y means if x and y not if x or y – Aluan Haddad Apr 14 '18 at 05:16
  • 1
    @Hamed I said "natural language" and in my initial comment I said "if this did work" (_if_ as in _suppose_). So, if `operator,` did actually compose boolean expressions, then it certainly would mean "and" as opposed to "or". – Aluan Haddad Apr 14 '18 at 05:24
  • @AluanHaddad Uh I see, Sorry English is not my strong suit so I had hard time getting what you mean :D – HMD Apr 14 '18 at 05:26
  • @Hamed no worries :D. I enjoy English, granted I'm also a native speaker, but I think this is true in most natural languages. I could be wrong and would love a counter example. – Aluan Haddad Apr 14 '18 at 05:29
  • 1
    @AluanHaddad That's also true in my language, "This, this and that". Good point to justify initial answer. – HMD Apr 14 '18 at 05:34
  • 1
    @Hamed yes exactly. I was trying to justify this answer as it was initially formulated. Since in C++ `operator,` is actually overloadable this isn't entirely academic ;) – Aluan Haddad Apr 14 '18 at 05:38
2

The comma operator should be replaced with "||" or "or". There are two basic ways the arguments can be related, one is the logical "and" ("&&") and one is the logical "or" ("||"). You use the first one when both conditions have to be true, and the second one if one of the conditions has to be true (and it doesn't matter if both of them are). If you put a comma operator, only the second argument is being taken into account, and that's not what you want.

FlatAssembler
  • 667
  • 7
  • 30
1

If you objective is to check if both password and username are correct, the if statement should look like this:

If( ( username.compare(“admin”) == 0) || ( password.compare(“therealpassword”) ==0) )

And to keep getting the user and password until the right ones comes. You could rewrite your code to this:

string username;
string password;

cout << "Hello there. To access this program, please put in your user name and password." << endl;
do{
    cout << "Username: ";
    cin >> username;
    cout << "Password: ";
    cin >> password;
    if( username.compare("admin") || password.compare(“therealpassword") ){
        cout << "Username or password is incorrect." << endl;
    }
}while( username.compare("admin") || password.compare(“therealpassword") );