question about strcmp and strtok -
Garwan50 - 20.10.2013
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
Re: question about strcmp and strtok -
PT - 20.10.2013
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;
}
Re : question about strcmp and strtok -
Garwan50 - 20.10.2013
don't like sscanf.
Thanks, but, the cmdtext[4] does'nt need the size ?
Re: question about strcmp and strtok -
Konstantinos - 20.10.2013
The 4 (lenght) in the strcmp line is not necessary and it will work even if you remove it.
Re : question about strcmp and strtok -
Garwan50 - 20.10.2013
I test this, and no :/
Re: question about strcmp and strtok -
Konstantinos - 20.10.2013
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.
AW: question about strcmp and strtok -
Nero_3D - 20.10.2013
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);
}
}
Re : question about strcmp and strtok -
Garwan50 - 20.10.2013
Thanks
.
What mean EOS ?
AW: question about strcmp and strtok -
Nero_3D - 20.10.2013
End of string, it is the same as 0