SA-MP Forums Archive
strcmp - 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: strcmp (/showthread.php?tid=287626)



strcmp - Jack_Leslie - 04.10.2011

pawn Код:
if(strcmp(CarInfo[i][vOwner], PlayerName(playerid)) == 0) return i;
It's meant to return an ID if the two strings match but it doesn't. Is there anything wrong with the strcmp?


Re: strcmp - mkr - 04.10.2011

if CarInfo[i][vOwner] or PlayerName(playerid) is an empty string, strcmp will act unexpectedly and tell you that they match. Otherwise, the strcmp looks fine. What does PlayerName look like? A debug message with the values of CarInfo[i][vOwner] and PlayerName(playerid) would be good.


Re: strcmp - Jack_Leslie - 04.10.2011

Well really I'm trying to make a code to loop my CarInfo, and if CarInfo[i][vOwner] matches the player name, then return i.


Re: strcmp - mkr - 04.10.2011

Is that the entire loop body? I assume CarInfo[i][vOwner] is never empty, but PlayerName may be spitting out empty strings if you're calling it for playerids that aren't connected.


Re: strcmp - Jack_Leslie - 04.10.2011

Nothing to do with playerids that aren't connected cause it asks if the playerid that just logged on, so it is connected. The loop would go like:
pawn Код:
for(new i=0; i<sizeof(CarInfo); i++)
    {
        //strcmp code here asking if playername matches CarInfo[i][vOwner]
    }



Re: strcmp - mkr - 04.10.2011

pawn Код:
for (new i = 0; i < sizeof(CarInfo); i++)
{
    if (strlen(CarInfo[i][vOwner]) && strcmp(CarInfo[i][vOwner],PlayerName(playerid)) == 0)
    {
        return i;
    }
}
Try that.


Re: strcmp - Jack_Leslie - 04.10.2011

Quote:
Originally Posted by mkr
Посмотреть сообщение
pawn Код:
for (new i = 0; i < sizeof(CarInfo); i++)
{
    if (strlen(CarInfo[i][vOwner]) && strcmp(CarInfo[i][vOwner],PlayerName(playerid)) == 0)
    {
        return i;
    }
}
Try that.
Didn't work.

Toni, I'm trying to return the ID from the loop where the vOwner and PlayerName match.


Re: strcmp - Toni - 04.10.2011

Quote:
Originally Posted by Jack_Leslie
Посмотреть сообщение
Didn't work.

Toni, I'm trying to return the ID from the loop where the vOwner and PlayerName match.
I'm still trying to get the point of the ID getting returned. Is a specific ID / player ID suppose to be returned? Because correct me if I'm wrong, you're looping through an array and just waiting for a match, and whatever match that is an ID is suppose to be returned?


Re: strcmp - Jack_Leslie - 04.10.2011

Quote:
Originally Posted by Toni
Посмотреть сообщение
I'm still trying to get the point of the ID getting returned. Is a specific ID / player ID suppose to be returned? Because correct me if I'm wrong, you're looping through an array and just waiting for a match, and whatever match that is an ID is suppose to be returned?
Yes I am looping through the arrays and waiting for the vOwner and PlayerName to match, if they do, return the id of that match (from the loop), if not, then return whatever else.


Re: strcmp - Toni - 04.10.2011

Quote:
Originally Posted by Jack_Leslie
Посмотреть сообщение
Yes I am looping through the arrays and waiting for the vOwner and PlayerName to match, if they do, return the id of that match (from the loop), if not, then return whatever else.
That still goes back to the strcmp issue; it should return a value (try using printf("%d", i) and see if it has an given output. You are checking for the name and might get a match, but the way strcmp was created might give a different output.