Strcmp still giving me hell!
#1

Okay so I have two codes using strcmp to compare two strings that are both kicking me in the balls.

pawn Код:
stock GetCarIDFromPlayer_Enum(playerid)
{
    for(new i=0; i<sizeof(CarInfo); i++)
    {
        if(!strcmp(CarInfo[i][vOwner], PlayerName(playerid), true)) return i;
        else if(strcmp(CarInfo[i][vOwner], PlayerName(playerid), true)) return 0;
    }
    return -1;
}
The problem with that code is that, if the two strings match, it still returns 0, when it should return i, but if I take out the else if bit and just have the first if .... return i; then it returns the i. The solution is to remove the else if, but I need to have that so it returns 0 if the two strings do not match.

pawn Код:
if(strcmp(tmp, gatepass) != 0)
        {
            SendClientMessage(playerid, COLOR_LIGHTRED, "* Dills gate: Incorrect password !");
            print("Incorrect pass");
            return 1;
        }
The problem with that code is that it does nothing. I have codes above it. I debugged it, and it stops at the bit of code. "Incorrect pass" doesn't print either.

I'll pay someone $10 if they can either fix this or make another code doing the same thing, I've requested help so many times and no one has been able to fix it >.>
Reply
#2

That's going to always return 0 because when i == 0 you check if the name matches, if it does it returns i (0) but then you have else return 0.
Reply
#3

Yeah but the name from i0 doesn't match the players name? so it shouldn't return 0
and if I remove the else if then it returns the correct i.
Reply
#4

pawn Код:
stock GetCarIDFromPlayer_Enum(playerid)
{
    for(new i=0; i<sizeof(CarInfo); i++) if(!strcmp(CarInfo[i][vOwner], PlayerName(playerid), true)) return i;
    return -1;
}
You don't need to return 0. For the second problem, make sure there is actually something in the strings.
Reply
#5

yes I do need to return 0 because I'm setting someones enum with that code, so if there is no match then I need to return 0 to set their enum to 0.
Reply
#6

You don't need the "else if" statement" your needlessly adding another string comparison. It either matches or doesn't (if your not checking which string comes alphabetically first). So just return zero at the end of the function.
pawn Код:
stock GetCarIDFromPlayer_Enum(playerid)
{
    for(new i=0; i<sizeof(CarInfo); i++)
    {
        if(!strcmp(CarInfo[i][vOwner], PlayerName(playerid)))
            return i;
    }
    return 0;
}
The problem with your version is that the function will always return a value on the first iteration.
Reply
#7

Quote:
Originally Posted by iggy1
Посмотреть сообщение
You don't need the "else if" statement" your needlessly adding another string comparison. It either matches or doesn't (if your not checking which string comes alphabetically first). So just return zero at the end of the function.
pawn Код:
stock GetCarIDFromPlayer_Enum(playerid)
{
    for(new i=0; i<sizeof(CarInfo); i++)
    {
        if(!strcmp(CarInfo[i][vOwner], PlayerName(playerid)))
            return i;
    }
    return 0;
}
The problem with your version is that the function will always return a value on the first iteration.
Okay I understand what you are saying, but that still doesn't work. I have that stock when I log in right, and it sets an enum, pVehicle, when the user logs in.

First off, I made sure the vOwner was my name, so it would match, and that worked. The ID of this was 51. So, when I logged in, it set my pVehicle to 51, like it was supposed too. However, I then logged off and changed the vOwner name, so when I logged back in the strings wouldn't match, and instead of setting my pVehicle to 0, it kept it at 51.
Reply
#8

Are you resetting the var when players disconnect? It seems like your not doing.

EDIT: Actually that might not be a problem. I can't tell w/o more code. Because the above code should work, your problem is elsewhere than the code you posted.
Reply
#9

Yeah I am, I check my user file and it's gone back to 0. Even if I didn't, it should set it back to 0 when I log in if the strings don't match which they don't.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)