Need help with Strcmp :S
#1

I got a problem with strcmp..

My Code:

Код:
if(strcmp(GetName(playerid), dini_Get(string, "Owner"), true))
{
if(CarInfo[CarID][Engine] == 0)
{
//Start engine
CarInfo[CarID][Engine] = 1;//This should happen if the name of the player = the owner of the car.
TogglePlayerControllable(playerid, 1);//This should happen if the name of the player = the owner of the car.
}
}
else
{
SendClientMessage(playerid, Red, "You have no keys for this car.");//And this should happen if the name of the player is not the owner.
}
And now my problem: All players can start the engine. How can I fix it ? Only the carowner should be able to start the engine...
Reply
#2

pawn Код:
if(!strcmp(GetName(playerid), dini_Get(string, "Owner"), true))
{
    ...
}
Reply
#3

Quote:
Originally Posted by exora
Посмотреть сообщение
pawn Код:
if(!strcmp(GetName(playerid), dini_Get(string, "Owner"), true))
{
    ...
}
Shouldn't it be like this:

pawn Код:
if(!strcmp(GetName(playerid)&&dini_Get(string, "Owner"), true)==1)
{
    ...
}
Correct me if im wrong.

Cheers.
Reply
#4

Quote:
Originally Posted by exora
Посмотреть сообщение
pawn Код:
if(!strcmp(GetName(playerid), dini_Get(string, "Owner"), true))
{
    ...
}
dont work now it always say: You have no keys for this car.

Quote:
Originally Posted by DeathOnaStick
Посмотреть сообщение
Shouldn't it be like this:

pawn Код:
if(!strcmp(GetName(playerid)&&dini_Get(string, "Owner"), true)==1)
{
    ...
}
Correct me if im wrong.

Cheers.
dont work..
Reply
#5

That is also correct, but I doubt it would compile (in theory it should). The only problem is that Strcmp can also return -1 (string2 is greater then string1 (it returns 1 on the opposite)), so it wouldn't work in all cases where the string wouldn't be the same.

pawn Код:
if(!strcmp(GetName(playerid), dini_Get(string, "Owner"), true))
{
    ...
}
Is the correct form for checking if the players name matches the owners one. Check if it saves in the file correctly
Reply
#6

Код:
if(!strcmp(GetName(playerid), dini_Get(string, "Owner"), true))
{
    if(CarInfo[CarID][Engine] == 0)
    {
        //Start engine
        CarInfo[CarID][Engine] = 1;//This should happen if the name of the player = the owner of the car.
        TogglePlayerControllable(playerid, 1);//This should happen if the name of the player = the owner of the car.
    }
}
else
{
    SendClientMessage(playerid, Red, "You have no keys for this car.");//And this should happen if the name of the  player is not the owner.
}
This should work.
Reply
#7

still dont work ..
and yes its saves in the file correct..
maybe its some other problem with the code.. here is my whole code for this function:

Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
if(newkeys == KEY_WALK)
	{
	if(IsPlayerInAnyVehicle(playerid))
	{
	if(GetPlayerVehicleSeat(playerid) == 0)
	{
	new CarID = GetPlayerVehicleID(playerid);
	new string[100];
	format(string, sizeof(string),"/Cars/Car(%d).Cfg", CarID);
	if(!strcmp(GetName(playerid), dini_Get(string, "Owner"), true))
	{
	if(CarInfo[CarID][Engine] == 0)
	{
	CarInfo[CarID][Engine] = 1;
	TogglePlayerControllable(playerid, 1);
	}
	}
	else
	{
	SendClientMessage(playerid, Red, "You have no keys for this car.");
	}
	}
	}
	return 1;
	}
return 1;
}
btw: getname function:

stock GetName(playerid)
{
new PlayerName[MAX_PLAYER_NAME];
GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
return PlayerName;
}
Reply
#8

pawn Код:
printf("%s", GetName(playerid));
                printf("%s", dini_Get(string, "Owner"));
                if(!strcmp(GetName(playerid), dini_Get(string, "Owner"), true))
                {
                    if(CarInfo[CarID][Engine] == 0)
                    {
                        CarInfo[CarID][Engine] = 1;
                        TogglePlayerControllable(playerid, 1);
                    }
                }
                else
                {
                    SendClientMessage(playerid, Red, "You have no keys for this car.");
                }
Try this to see if the printed names match. Because if they do, then I have no idea why strcmp fails
Reply
#9

lol -.-

[19:35:35] PlayerName: [Nino]
[19:35:35] CarOwner: [Nino]
Reply
#10

Are you sure CarInfo[CarID][Engine] is 0?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)