question about strcmp and strtok
#1

Hi, i just have a question to ask you guys:

I wrote a simple command that send the playerid's text to everyone.
So the text parameter is stocked is the text variable.
But if i delete the 4 in the strcmp, the command will not work.

pawn Код:
if(strcmp("/all", cmdtext, true, 4) == 0) // Removing the 4 make the command to not work.
        {
            new text[124];
            new str[124];
            new playername[MAX_PLAYER_NAME];
            new idx;
            text = strtok(cmdtext[4], idx); // 4 is here too.
            if (strlen(text) == 0)
            {
                SendClientMessage(playerid, 0x41A5F5FF, "[USE] /all <text>");
                return 1;
            }
            else
            {
                GetPlayerName(playerid, playername, sizeof(playername));
                RPName(playername);
                format(str, sizeof(str), "[ALL] {00CED1}%s {FAF427}say: %s", playername, text);
                SendClientMessageToAll(COLOR_YELLOW, str);
                return 1;
            }  
        }
And i wanted to know why it does'nt work without the 4.

Thanks
Reply
#2

because you need put the size of command...


use sscanf is better, look

pawn Код:
if(strcmp("/all", cmdtext, true, 4) == 0)
{
    new text[124];
    if(sscanf(cmdtext, "s[4]s[124]", cmd, text)) return SendClientMessage(playerid, 0x41A5F5FF, "[USE] /all <text>");
   
    new str[124], playername[MAX_PLAYER_NAME];
    GetPlayerName(playerid, playername, sizeof(playername));
    RPName(playername);
    format(str, sizeof(str), "[ALL] {00CED1}%s {FAF427}say: %s", playername, text);
    SendClientMessageToAll(COLOR_YELLOW, str);
    return 1;
}
Reply
#3

don't like sscanf.

Thanks, but, the cmdtext[4] does'nt need the size ?
Reply
#4

The 4 (lenght) in the strcmp line is not necessary and it will work even if you remove it.
Reply
#5

I test this, and no :/
Reply
#6

https://sampwiki.blast.hk/wiki/Strcmp

length (optional)*

I've been using strcmp for a long time and I don't use the lenght at all since it's optional. I'm sure it'll work. Unless you remove the 4 from cmdtext.
Reply
#7

Lets say you input something like "/all Hello, my name is Garwan50!"
Now you try to check if that compares to "/all" with this line
pawn Код:
if(strcmp("/all", "/all Hello, my name is Garwan50!", true) == 0) // returns -28
They clearly doesn't match therefor you need the 4, than strcmp will stop comparing after the forth character

pawn Код:
//
    if(strcmp("/all", cmdtext, true, 4) == 0) {
        static const
            text[] = "[USE] /all <text>"
        ;
        if(cmdtext[4] == ' ') {
            if(cmdtext[5] == EOS) {
                return SendClientMessage(playerid, 0x41A5F5FF, text);
            }
            new
                tmp[144]
            ;
            GetPlayerName(playerid, tmp, MAX_PLAYER_NAME);
            RPName(tmp);
            format(tmp, sizeof tmp, "[ALL] {00CED1}%s {FAF427}say: %s", tmp, cmdtext[5]);
            return SendClientMessageToAll(COLOR_YELLOW, tmp);
        }
        if(cmdtext[4] == EOS) {
            return SendClientMessage(playerid, 0x41A5F5FF, text);
        }
    }
Reply
#8

Thanks .

What mean EOS ?
Reply
#9

End of string, it is the same as 0
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)