SA-MP Forums Archive
[CODE] if statements with strings - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: [CODE] if statements with strings (/showthread.php?tid=331591)



[CODE] if statements with strings - help please - squomp - 05.04.2012

//Buy Car//////////////////////////////////////////////////////////////
dcmd_buycar(playerid, params[])
{
static carName[32], Bravura[32], Glendale[32];
Glendale = "Glendale";
Bravura = "Bravura";
if (sscanf(params, "s", carName)) return SendClientMessage(playerid, red, "Usage: /buycar [car name]");
if(IsPlayerInRangeOfPoint(playerid, 5, 1667.5986,-1887.8107,13.2885))
{
if(carName == Bravura)
{
if (GetPlayerMoney(playerid) >= 5000)
{
GivePlayerMoney(playerid, - 5000);
CreateVehicle(401,1667.5986,-1887.8107,13.2885,89.9480,78,76,0);
vehicleID ++;
playerVeh = vehicleID;
new VehString[128];
format(VehString, 128, "You are now using vehicle %i", playerVeh);
SendClientMessage(playerid, 0xFFFFFFFF, VehString);
return 1;
}
}
else
{
SendClientMessage(playerid, 0xFFFFFFFF, "You do not have enough money to buy this");
return 1;
}

if(carName == Glendale)
{
if (GetPlayerMoney(playerid) >= 2500)
{
GivePlayerMoney(playerid, - 2500);
CreateVehicle(466,1665.6439,-1899.6490,13.3311,1.4440,78,76,0);
vehicleID ++;
playerVeh = vehicleID;
new VehString[128];
format(VehString, 128, "You are now using vehicle %i", playerVeh);
SendClientMessage(playerid, 0xFFFFFFFF, VehString);
return 1;
}
}

else
{
SendClientMessage(playerid, 0xFFFFFFFF, "You do not have enough money to buy this");
return 1;
}
return 1;
}
return 1;
}
/////////////////////////////////////////////////////////////////////

how do I get this to work, the error occurs when I try -
if(carName == Glendale)
I get error
C:\Users\Keiren\Desktop\Used\Games\GTA San Andreas\samp\gamemodes\aussieroleplay2.pwn(535) : error 033: array must be indexed (variable "carName")


Re: [CODE] if statements with strings - help please - DR3AD - 05.04.2012

Man, I understand what you trying to do.. in other languages that would work a think, at least some of it

Код:
format(Bravura, sizeof(Bravura), "Bravura");
format(Glendable, sizeof(Glendale), "Glendale");    // this instead the "=" operator to give them a value
Then you have to do the same thing for carName
Код:
format(carName, sizeof(carName), "%s", params);
And to compare the two, you use
Код:
strcmp(Glendale, carName, true);   //and not the "=="
Hope I helped you somehow


Re: [CODE] if statements with strings - squomp - 05.04.2012

yeah thanks but when I do -
if(strcmp(Glendale, carName, true));
CreateVehicle(401,1667.5986,-1887.8107,13.2885,89.9480,78,76,0);
if(strcmp(Bravura, carName, true));
CreateVehicle(401,1667.5986,-1887.8107,13.2885,89.9480,78,76,0);

the two get mixed up and Glendale creates bravura and Bravura creates Glendale so I then have to do -

if(strcmp(Bravura, carName, true));
CreateVehicle(466,1667.5986,-1887.8107,13.2885,89.9480,78,76,0);
if(strcmp(Glendale, carName, true));
CreateVehicle(401,1667.5986,-1887.8107,13.2885,89.9480,78,76,0);


Re: [CODE] if statements with strings - DR3AD - 05.04.2012

The strcmp() returns a value if the values match and another if they dont

Like this
Код:
 if(strcmp(Glendale, carName, true) == 0)
               // then you create a glendale
         else if(strcmp(Bravura, carName, true == 0)
              // then you create a bravura
         else
              // The car isnt a bravura or a glendale
You can see here when strcmp returns true https://sampwiki.blast.hk/wiki/Strcmp
"0" means that they are equal

you can't use two if's because it will do the two if's and thats not what you want
if it is a bravura, you can a piece of code, else if it is a glendale you run other piece of code and if it isn't a bravura or a glendale (the "else" part), then you can for example send a message saying "Invalid vehicle


Re: [CODE] if statements with strings - Harish - 05.04.2012

you used a static CarName[32];

static function will store the last used value ever..so try using new CarName[32];

and i had no idea where you placed that CarName = params;


Re: [CODE] if statements with strings - squomp - 05.04.2012

okay thanks for the help guys


Re: [CODE] if statements with strings - squomp - 05.04.2012

I see why it wasnt working its because I had to do - if(strcmp(Glendale, carName, true) == 0)

but i was doing - if(strcmp(Glendale, carName, true))