sscanf - optional number?
#1

How do you have an optional number with sscanf?

I have,
pawn Код:
new action[128], item[128], amount;
   
    if(sscanf(params, "s[128]z[128]d", action, item, amount))
    {
        SendClientMessage(playerid, COLOR_GREY, "* Usuage: /car [syntax]");
        SendClientMessage(playerid, COLOR_GRAD2, "* Syntax: Check");
        SendClientMessage(playerid, COLOR_GRAD3, "* Syntax: Put - pot crack mats weapon");
        SendClientMessage(playerid, COLOR_GRAD4, "* Syntax: Get - pot crack mats weapon");
        return 1;
    }
And then,
pawn Код:
if(strcmp(action, "check") == 0)
    {
        if(!amount)
        {
            new pot, crack, mats, gun1, gun2, gun3;
            pot = CarInfo[car][vPot], crack = CarInfo[car][vCrack], mats = CarInfo[car][vMats];
            SendClientMessage(playerid, COLOR_GREY, "__________ Car Storage __________");
            format(string, sizeof(string), "Crack: %d - Pot: %d - Materials: %d", crack, pot, mats);
            SendClientMessage(playerid, COLOR_GRAD2, string);
        }
        return 1;
    }
But if I do /car check then it doesn't work..
Reply
#2

pawn Код:
if(sscanf(params, "S[128]Z[128]D", action, item, amount))
Just put caps on the param(s) that you want to be optional.
Reply
#3

Cheers mate, that worked. How do I check if nothing is entered in the option string if I want something to be entered?

I tried:
pawn Код:
if(!strlen(item)) return SendClientMessage(playerid, COLOR_GREY, "* Usuage: /car put [item] [amount]");
But it didn't work, I also tried just if(!item) but that didn't work neither.
Reply
#4

You mean you want default values on the params? or just check if params is null?
Reply
#5

Use
pawn Код:
if(isnull(params))
Reply
#6

Default values are enclosed in parentheses.
pawn Код:
if(sscanf(params, "S[128](default string)S[128](another string)D(500)", action, item, amount))
Result is; action = "default string", item = "another string" and amount = 500. If no params were entered. Also assuming your using the plugin.

Quote:
Originally Posted by [MWR]Blood
Посмотреть сообщение
Use
pawn Код:
if(isnull(params))
He is already using sscanfs inbuilt null check in his code...
Reply
#7

Quote:
Originally Posted by iggy1
Посмотреть сообщение
Default values are enclosed in parentheses.
pawn Код:
if(sscanf(params, "S[128](default string)S[128](another string)D(500)", action, item, amount))
Result is; action = "default string", item = "another string" and amount = 500. If no params were entered. Also assuming your using the plugin.
action = needed string
item = optional string
amount = needed value if item is entered

I get undefined symbol on isnull, why? :S
Reply
#8

Quote:
Originally Posted by Jack_Leslie
Посмотреть сообщение
action = needed string
item = optional string
amount = needed value if item is entered

I get undefined symbol on isnull, why? :S
DW your sscanf line IS checking if params is null. You dont need to use "isnull" in this case. BTW which command processor are you using?
Reply
#9

Quote:
Originally Posted by iggy1
Посмотреть сообщение
DW your sscanf line IS checking if params is null. You dont need to use "isnull" in this case.
Oh right okay so I'm not using isnull, however, if I put a action item and no amount, the default amount is 114, but it should send the message syntax: /car put or whatever I want to send?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)