STRCMP help
#1

I really hope I'm not missing something obvious here, but for some reason it keeps telling me I have an argument type mismatch in my code.

Код:
cmd:goto(playerid, params[])
{
	new location;
	if(!PlayerData[playerid][p_LOGGED_IN]) return false;
    if(PlayerData[playerid][p_ADMINLVL] < 0) return SendClientMessage(playerid, WHITE, CMD_PERM);
    if( sscanf( params, "s", location ) ){
		SendClientMessage(playerid, WHITE, CMD_USAGE"/goto [location]" );
		SendClientMessage(playerid, WHITE, "Locations: gym, lspd, stadium");
		return true;
	}
    else
    {
        if(!strcmp(location, "gym")){
			SetPlayerPos(playerid, 2226.1499,-1722.0465,13.5608);
		} else {
		    SendClientMessage(playerid, WHITE, "Invalid Location");
		}
    }
    return true;
}
Error:
Код:
Script.pwn(590) : error 035: argument type mismatch (argument 1)
Line 590
Код:
if(!strcmp(location, "gym")){
AFAIK STRCMP is string compare so I'm comparing location (which is the user's input) to the string "gym"

Additionally, if there is another way to do such a command instead of doing if/elseif/else statements that'd be nice. Researched and found strings cannot be used as values inside switch statements which would have been really easy.
Reply
#2

The variable 'location' is not declared as an array and thus can't store strings or be used in strcmp.
Reply
#3

Just adding here, but wouldn't Strfind Work best here?

PHP код:
if(strfind(params"gym")) //... 
Reply
#4

Quote:
Originally Posted by ISmokezU
Посмотреть сообщение
Just adding here, but wouldn't Strfind Work best here?

PHP код:
if(strfind(location"gym")) //... 
Depends. If you have multiple gym locations:
PHP код:
if(strfind(params"gym") != -1) {
    
//code
}
else if(
strfind(location"gym2") != -1) {
    
// code

If you now type in the command "gym2" or "gym3", it will still execute the first if statement.

And if you did something in the lines of:
PHP код:
if(strfind(params"gym") != -1) {
    
//code
}
if(
strfind(location"gym2") != -1) {
    
// code

And then enter in the command: gymgym2, it will execute both if blocks which is presumably unintentional behavior and thus is not correct.
Reply
#5

Quote:
Originally Posted by AndySedeyn
Посмотреть сообщение
The variable 'location' is not declared as an array and thus can't store strings or be used in strcmp.
So it has to be an array? I thought it was just two strings, fixed the issue when I turned it into an array!

Quote:
Originally Posted by AndySedeyn
Посмотреть сообщение
Depends. '123gym' and 'ezoigjegym' will both return an index when strfinding for 'gym'. If you have multiple gym locations, then it will return the code under the first if-statement that the check is first used:
PHP код:
if(strfind(params"gym") != -1) {
    
//code
}
else if(
strfind(location"gym2") != -1) {
    
// code

If you now type in the command "gym2" or "gym3", it will always execute the code under the first if statement. If you were to change the else-if statement to an if statement, then it will always execute the last if statement that it is checked against
Probably don't wanna go down that road then. Just turned my location variable to an array, works perfectly fine so thanks for that!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)