Check if a string exists in an array.
#1

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 
isizeof(cityWords); i++)
        {
            if(!
strcmp(inputtextcityWords[i], true))
            {
                
SendClientMessage(playerid, -1"You found one of the hidden cities!");
                
GivePlayerMoney(playerid10000);
                break;
            }
            else if(
== 5)
            {
                
SendClientMessage(playerid, -1"This hidden city doesn't exist. Try again!");
                
ShowPlayerDialog(playeridDIALOG_QUIZ_2DIALOG_STYLE_INPUT"Hidden City""Find one of the hidden cities and win a prize!""OK""Cancel");
                break;
            }
        }
    }
    return 
1;

Reply
#2

You didn't mention what not working.Although taking a quick glance at your code i saw this in the loop.
PHP код:
else if(== 5
            { 
                
SendClientMessage(playerid, -1"This hidden city doesn't exist. Try again!"); 
                
ShowPlayerDialog(playeridDIALOG_QUIZ_2DIALOG_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.
Reply
#3

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 
0sizeof(g_Texts); x++)
{
    if(
strcmp(inputtextg_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!");

Reply
#4

Thanks for helping me out! It worked! (+REP to both)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)