sscanf multiple optional parameters
#1

I have a command to create and manage businesses, with the parameters being task (string), business ID (integer) and business name (string).

pawn Код:
if(sscanf(params, "s[12]I(-1)S(null)[20]", task, id, name))
The business ID parameter isn't used for all tasks, hence it being optional, and the name parameter is only used for the task related to naming a business, so it's optional as well.

This all works fine but I've tried to add another optional parameter at the end (hour), to use with two new tasks that allow you to set the opening and closing hours for the business.

pawn Код:
if(sscanf(params, "s[12]I(-1)S(null)[20]I(-1)", task, id, name, hour))
When I enter "/abusiness closingtime 2 13", it puts "13" in the name parameter.

So how do I properly add this optional parameter at the end so that if my task is closingtime or openingtime, it skips the name parameter?
Reply
#2

I think you need use
/abusiness closingtime 2 null 13
or change the order
if(sscanf(params, "s[12]I(-1)I(-1)S(null)[20]", task, id, hour, name))
Reply
#3

Thanks for the reply but I found an alternative to what you suggested. It's probably bad practice or something but I used this:
pawn Код:
if(sscanf(params, "s[12]I(-1)I(-1)", task, id, hour))
after:
pawn Код:
else if(!strcmp(task, "openingtime", true))
and
pawn Код:
else if(!strcmp(task, "closingtime", true))
So essentially skipped the name parameter when I knew what task they had typed. It ends up assigning the hour to both the name parameter and hour parameter.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)