strcmp
#1

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?
Reply
#2

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.
Reply
#3

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.
Reply
#4

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.
Reply
#5

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]
    }
Reply
#6

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.
Reply
#7

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.
Reply
#8

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?
Reply
#9

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.
Reply
#10

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)