Check if a string exists in an array. - 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: Check if a string exists in an array. (
/showthread.php?tid=633661)
Check if a string exists in an array. -
ChristolisTV - 05.05.2017
Hello! I'm making a server but I have a problem. How can I check if a string (inputtext) exists in an array? I have tried this but it's not working:
PHP код:
new cityWords[][] = { //Beta
"Shiopross City",
"Cuacaster City",
"Dine City",
"Looton City"
};
if(dialogid == DIALOG_QUIZ_2)
{
if(response)
{
for(new i; i < sizeof(cityWords); i++)
{
if(!strcmp(inputtext, cityWords[i], true))
{
SendClientMessage(playerid, -1, "You found one of the hidden cities!");
GivePlayerMoney(playerid, 10000);
break;
}
else if(i == 5)
{
SendClientMessage(playerid, -1, "This hidden city doesn't exist. Try again!");
ShowPlayerDialog(playerid, DIALOG_QUIZ_2, DIALOG_STYLE_INPUT, "Hidden City", "Find one of the hidden cities and win a prize!", "OK", "Cancel");
break;
}
}
}
return 1;
}
Re: Check if a string exists in an array. -
SyS - 05.05.2017
You didn't mention what not working.Although taking a quick glance at your code i saw this in the loop.
PHP код:
else if(i == 5)
{
SendClientMessage(playerid, -1, "This hidden city doesn't exist. Try again!");
ShowPlayerDialog(playerid, DIALOG_QUIZ_2, DIALOG_STYLE_INPUT, "Hidden City", "Find one of the hidden cities and win a prize!", "OK", "Cancel");
break;
}
if i becomes 5 it violate loop rule and the above part will not work.Put that statement outside the loop.
Re: Check if a string exists in an array. -
RIDE2DAY - 05.05.2017
SyS is right. Anyway, the next code worked perfectly for me:
PHP код:
new g_Texts[3][6] = { /* 3 rows - 6 characters as max. per row */
{"hi"},
{"hello"},
{"bye"}
};
new inputtext[6] = "hello";
new bool:found;
for(new x = 0; x < sizeof(g_Texts); x++)
{
if(strcmp(inputtext, g_Texts[x], true) == 0)
{
found = true;
SendClientMessage(playerid, -1, "You found one of the hidden cities!");
break;
}
}
if(!found)
{
SendClientMessage(playerid, -1, "This hidden city doesn't exist. Try again!");
}
Re: Check if a string exists in an array. -
ChristolisTV - 05.05.2017
Thanks for helping me out! It worked!

(+REP to both)