strcmp
#1

Ok, I have problem with strcmp. So, for example:

I have command smth like this: /a weapon id gunid ammount

And, everything is okay, just strcmp is fucked up.

cmd example:

pawn Код:
CMD:a( playerid, params[ ] )
{
if( ! strcmp( params, "weapon", true ) )
{
....................
}
else if( ! strcmp( params, "ban", true ) )
{
......................
}
else
{
SendClientMessage( playerid, COLOR, "/a [weapon/ban]" );
}
return true;
}
and when I type in game /a weapon [another params] I get SendClientMessage( playerid, COLOR, "/a [weapon/ban]" );
Reply
#2

Delete the else and add this to the top:

pawn Код:
if(!strlen(params)) return SendClientMessage(playerid,COLOR_RED,"[Error]: Use /a[weapon/ban]");
Reply
#3

You mean after

else if( ! strcmp( params, "ban", true ) )
{
......................
}
Reply
#4

Since you use zcmd, I assume you also have sscanf2 included.
pawn Код:
CMD:a(playerid, params[])
{
    new
        var[16];
    sscanf(params, "s[16] ", var);
    if(!strcmp(var, "weapon", true))
    {
        new
            id, weaponid, ammo;
        if(sscanf(params[strlen(var)], "udd", id, weaponid, ammo)) return SendClientMessage(playerid, WHITE, "Use: /a weapon [id] [weaponid] [ammo]");
        //if that doesn't work, try params[strlen(var) + 1]
    }
    else if(!strcmp(var, "ban", true))
    {

    }
    else SendClientMessage(playerid, WHITE, "Use: /a [weapon/ban]");
    return 1;
}
Reply
#5

But, I using strcmp in onplayertext and it works good, and in cmd not?..

pawn Код:
COMMAND:pinigai( playerid, params[ ] )
{
    if( GetPVarInt( playerid, "Administratorius" ) < 3 ) return SendClientMessage( playerid, -1, ""#h_Balta"[ > ] "#h_Zalia"Jыs neesate "#h_Geltona"Administratorius" );
    if( !strcmp( params, "nuiimti", true ) )
    {
        new
            ID,
            Kiekis
        ;
        if( sscanf( params, "ud", ID, Kiekis ) ) return SendClientMessage( playerid, -1, ""#h_Balta"[ > ] "#h_Zalia"/pinigai nuiimti "#h_Geltona"<Vardas> <Kiekis>" );
        if( ID == INVALID_PLAYER_ID ) return SendClientMessage( playerid, -1, ""#h_Balta"[ > ] "#h_Geltona"Юaidлjas yra "#h_Raudona"neprisijungжs" );
        if( Kiekis > GetPlayerMoneyEx( playerid ) ) return SendClientMessage( playerid, -1, ""#h_Balta"[ > ] "#h_Geltona"Kiekis suvestas "#h_Raudona"blogai" );
        new
            Stringas[ 160 ]
        ;
        format( Stringas, 160, ""#h_Balta"[ > ] "#h_Geltona"Administratorius "#h_Zalia"%s "#h_Raudona"nuemл Jums pinigш. "#h_Orandzine"Kiekis: %d", ZaidejoVardas( playerid ), Kiekis );
        SendClientMessage( ID, -1, Stringas );
        GivePlayerMoneyEx( ID, - Kiekis );
    }
    if( !strcmp( params, "prideti", true ) )
    {
        new
            ID,
            Kiekis
        ;
        if( sscanf( params, "ud", ID, Kiekis ) ) return SendClientMessage( playerid, -1, ""#h_Balta"[ > ] "#h_Zalia"/pinigai prideti "#h_Geltona"<Vardas> <Kiekis>" );
        else if( ID == INVALID_PLAYER_ID ) return SendClientMessage( playerid, -1, ""#h_Balta"[ > ] "#h_Geltona"Юaidлjas yra "#h_Raudona"neprisijungжs" );
        new
            Stringas[ 159 ]
        ;
        format( Stringas, 159, ""#h_Balta"[ > ] "#h_Geltona"Administratorius "#h_Zalia"%s "#h_Raudona"davл Jums pinigш. "#h_Orandzine"Kiekis: %d", ZaidejoVardas( playerid ), Kiekis );
        SendClientMessage( ID, -1, Stringas );
        GivePlayerMoneyEx( ID, Kiekis );
    }
    else
    {
        SendClientMessage( playerid, -1, ""#h_Balta"[ > ] "#h_Zalia"/pinigai "#h_Geltona"nuiimti/prideti" );
    }
    return true;
}
I dont want to use another ways, I just wanna know whats wrong in my strcmp using;<>
Reply
#6

Strings don't match if you type something like "/a weapon 22 22 500", it compares "weapon" with "weapon 22 22 500".
Reply
#7

Quote:
Originally Posted by Gh0sT_
Посмотреть сообщение
Ok, I have problem with strcmp. So, for example:

I have command smth like this: /a weapon id gunid ammount

And, everything is okay, just strcmp is fucked up.

cmd example:

pawn Код:
CMD:a( playerid, params[ ] )
{
if( ! strcmp( params, "weapon", true ) )
{
....................
}
else if( ! strcmp( params, "ban", true ) )
{
......................
}
else
{
SendClientMessage( playerid, COLOR, "/a [weapon/ban]" );
}
return true;
}
and when I type in game /a weapon [another params] I get SendClientMessage( playerid, COLOR, "/a [weapon/ban]" );
There's nothing wrong with strcmp, it's your logic. Look at what's happening for a second when you type this:

/a weapon id

pawn Код:
if( ! strcmp( "weapon id", "weapon", true ) )
Obviously they aren't going to match! You need to split the parameters by spaces and take out each parameter for each separate part of the command. Use a function like sscanf.

Person above beat me to it
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)