strcmp // !strcmp
#1

Hello,
I looked at the codes of register and login sytem
and could not understand why they do this - with " ! " in the strcmp.

Код:
      case DLOGIN:
        {
           	    if (!response) return Kick(playerid);
           	    if(!strlen(inputtext)) return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,"LOGIN","YOU DONT ENTER PASS","LOGIN","EXIT");
                if(!strcmp(inputtext,pInfo[playerid][pPassword],true))
                {
				SendClientMessage(playerid, -1, "YOU ARE LOGGED.");
				}
                else return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,"LOGIN","YOU DONT LOGGED","LOGIN","EXIT");
                return 1;
            }
    }
    return 1;
}
I dont understand why he use in "!strcmp" im beginner scripted... i learn

Thanks to the helpers.
Reply
#2

https://sampwiki.blast.hk/wiki/Strcmp
Reply
#3

if(!strcmp(inputtext,pInfo[playerid][pPassword],true))
this ! means in strcmp that the string matched and if u don't use this ! it mean what happens if the string does not match
Reply
#4

'!' - If the outcome of the conditional statement is true, then it will be false and vice-versa.

Kind of a bad explanation...

pawn Код:
new one; // one is 0
if(!one) {
    // this will go through
}

one = 1;
if(!one) {
    // this will not go through
}
If we did on‌e = 1, then the if statement would be considered false and move on.

Edit: found a better explanation.

https://wiki.alliedmods.net/Pawn_tutorial#If_Statements (nothing to do with SA-MP, still good resource)

Reply
#5

Return Values:
-1 if string1 comes before string2
1 if string1 comes after string2
0 if the strings are the same (for the matched length).


! means 0
Reply
#6

The above posts explain the way the "not"-operator works, however to know why !strcmp does what you want, here is some extra info:

"!" makes the given value the opposite boolean. A boolean can only be true or false.

When an integer is cast to a boolean, "0"-integers are false and all other values are true. As "!" gives the opposite boolean, "!0" is true and "![any number]" is false.

strcmp -like the wiki says- returns 0 when the strings are exactly the same. therefore !strcmp(...) returns true when the two strings are exactly the same.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)