PDA

View Full Version : STRCMP help


BR3TT
26/03/2017, 03:51 AM
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.

AndySedeyn
26/03/2017, 04:09 AM
The variable 'location' is not declared as an array and thus can't store strings or be used in strcmp.

ISmokezU
26/03/2017, 04:19 AM
Just adding here, but wouldn't Strfind (http://wiki.sa-mp.com/wiki/Strfind) Work best here?

if(strfind(params, "gym")) //...

AndySedeyn
26/03/2017, 04:25 AM
Just adding here, but wouldn't Strfind (http://wiki.sa-mp.com/wiki/Strfind) Work best here?

if(strfind(location, "gym")) //...

Depends. If you have multiple gym locations:
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:
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.

BR3TT
26/03/2017, 04:39 AM
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!

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:
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!