sscanf integer after string
#1

So I'm working on this /taxi command.

Supposedly if you do /taxi fare <number>, it should update a variable.

But I found out what I'm doing is wrong, what could the alternative for this be?

Код:
CMD:taxi(playerid, params[])
{
	new section[5];
	if(sscanf(params, "s[5]", section))
	{
		SendUsageMessage(playerid, "/taxi [section]");
		SendClientMessage(playerid, COLOR_LIGHTBLUE, "Sections:{FFFFFF} Duty | Fare | UPCOMING");
		return 1;
	}
	else
	{
		if(strmatch(section, "fare"))
		{
			new farePrice;
			if(sscanf(section, "i", farePrice)) return SendUsageMessage(playerid, "/fare (fare price per second)");
			if(farePrice < 1 || farePrice > 10) return SendErrorMessage(playerid, "Your fare must be between $1 and $10.");
			playerFare[playerid] = farePrice;

			new str[50]; 
			format(str, sizeof(str), "You have set your fare to $%i per second.", farePrice);
			SendMessage(playerid, COLOR_YELLOW, str);
			return 1;
		}
		if(strmatch(section, "duty"))
		{
			// code goes here - this one works however since no sscanf is called after this
		}
	}
	return 1;
}
Reply
#2

If I do get what you mean. This should work:

Код:
CMD:taxi(playerid, params[])
{
	new taxiparam[10];
	if(sscanf(params, "s[10]", taxiparam)) return SCM(playerid, COLOR_GRAY, "USAGE: /taxi [Section] | Sections: Duty, Fare, TOBEADDED");
	if(!strcmp(taxiparam, "fare", true, 4))
	{
		new newfare, string[128];
		if(sscanf(params, "s[10]i", taxiparam, newfare)) return SCM(playerid, COLOR_GRAY, "USAGE: /taxi fare [Amount]");
		if(newfare < 1 || newfare > 10) return SendErrorMessage(playerid, "Your fare must be between $1 and $10.");
		playerFare[playerid] = newfare; 
		format(string, sizeof(string), "You have set your fare to $%i per second.", newfare);
		SendMessage(playerid, COLOR_YELLOW, string);	    
	}
        if(!strcmp(taxiparam, "duty", true, 4))
	{
		//addcode    
	}
        return 1;
}
Reply
#3

pawn Код:
CMD:taxi(playerid, params[])
{
    if (isnull(params))
    {
        SendUsageMessage(playerid, "/taxi [section]");
        SendClientMessage(playerid, COLOR_LIGHTBLUE, "Sections:{FFFFFF} Duty | Fare | UPCOMING");
        return 1;
    }

    // if (!strcmp(params, "option", ignorecase, LENGTH OF OPTION [6 here]))

    if (!strcmp(params, "fare", true, 4))
    {
        if (params[4] != ' ' && params[4] != '\0')
        {
            SendUsageMessage(playerid, "/taxi [section]");
            return SendClientMessage(playerid, COLOR_LIGHTBLUE, "Sections:{FFFFFF} Duty | Fare | UPCOMING");
        }
       
        new farePrice;
        // params[LENGTH OF OPTION + 1] (5 here)
        if(sscanf(params[5], "i", farePrice)) return SendUsageMessage(playerid, "/fare (fare price per second)");
        if(farePrice < 1 || farePrice > 10) return SendErrorMessage(playerid, "Your fare must be between $1 and $10.");
        playerFare[playerid] = farePrice;

        new str[50];
        format(str, sizeof(str), "You have set your fare to $%i per second.", farePrice);
        SendMessage(playerid, COLOR_YELLOW, str);
        return 1;
    }
    if (!strcmp(params, "duty", true, 4))
    {
        if (params[4] != ' ' && params[4] != '\0')
        {
            SendUsageMessage(playerid, "/taxi [section]");
            return SendClientMessage(playerid, COLOR_LIGHTBLUE, "Sections:{FFFFFF} Duty | Fare | UPCOMING");
        }
        return 1;
    }
    return 1;
}
It might look hard, but this is the most efficient way of doing that (AFAIK).
Reply
#4

Quote:
Originally Posted by thefirestate
Посмотреть сообщение
If I do get what you mean. This should work:

Код:
CMD:taxi(playerid, params[])
{
	new taxiparam[10];
	if(sscanf(params, "s[10]", taxiparam)) return SCM(playerid, COLOR_GRAY, "USAGE: /taxi [Section] | Sections: Duty, Fare, TOBEADDED");
	if(!strcmp(taxiparam, "fare", true, 4))
	{
		new newfare, string[128];
		if(sscanf(params, "s[10]i", taxiparam, newfare)) return SCM(playerid, COLOR_GRAY, "USAGE: /taxi fare [Amount]");
		if(newfare < 1 || newfare > 10) return SendErrorMessage(playerid, "Your fare must be between $1 and $10.");
		playerFare[playerid] = newfare; 
		format(string, sizeof(string), "You have set your fare to $%i per second.", newfare);
		SendMessage(playerid, COLOR_YELLOW, string);	    
	}
        if(!strcmp(taxiparam, "duty", true, 4))
	{
		//addcode    
	}
        return 1;
}
worked, cheers
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)