SA-MP Forums Archive
Problem with a loop - 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: Problem with a loop (/showthread.php?tid=603294)



Problem with a loop - Jack_Leslie - 21.03.2016

Hi, I'm having an issue with a loop! I've been trying to persist with fixing it myself for hours and I just can't figure it out.. it's probably something small

Code:
pawn Код:
for(new i = 0; i < sizeof(personalVehicleData); i++)
    {
        if(!strcmp(personalVehicleData[i][owner], playersName[playerid], false))
        {
            if(personalVehicleData[i][spawned] == 0)
            {
                printf("%d: %s, %d", i, personalVehicleData[i][owner], personalVehicleData[i][modelID]);
                vNumbers ++;
                format(string, sizeof(string), "%s\n%s", string, GetVehicleNameFromModel(personalVehicleData[i][modelID]));
            }
        }
    }
It's obviously meant to select the data where the names match, but it's selecting all of the data, weird thing is the data it selects where the names don't match, is empty.

Example:
[19:43:30] 0: Jack Leslie, 411
[19:43:30] 1: Jack Leslie, 415
[19:43:30] 2: Jack Leslie, 444
[19:43:30] 4: , 0 <-- Empty

Help is appreciated !


Re: Problem with a loop - K0P - 21.03.2016

Try:
Код:
	new Name;
	Name = personalVehicleData[i][owner];
	
	for(new i = 0; i < sizeof(personalVehicleData); i++)
	{
		if(!strcmp(GetName(playerid), Name, false))
		{
		    if(personalVehicleData[i][spawned] == 0)
		    {
			    printf("%d: %s, %d", i, personalVehicleData[i][owner], personalVehicleData[i][modelID]);
			    vNumbers ++;
			    format(string, sizeof(string), "%s\n%s", string, GetVehicleNameFromModel(personalVehicleData[i][modelID]));
		    }
		}
	}
	
	stock GetName(playerid)
	{
		new Name;
		GetPlayerName(playerid, Name, sizeof(Name));
		return 1;
	}



Re: Problem with a loop - Jack_Leslie - 21.03.2016

Quote:
Originally Posted by K0P
Посмотреть сообщение
Try:
Код:
	new Name;
	Name = personalVehicleData[i][owner];
	
	for(new i = 0; i < sizeof(personalVehicleData); i++)
	{
		if(!strcmp(GetName(playerid), Name, false))
		{
		    if(personalVehicleData[i][spawned] == 0)
		    {
			    printf("%d: %s, %d", i, personalVehicleData[i][owner], personalVehicleData[i][modelID]);
			    vNumbers ++;
			    format(string, sizeof(string), "%s\n%s", string, GetVehicleNameFromModel(personalVehicleData[i][modelID]));
		    }
		}
	}
	
	stock GetName(playerid)
	{
		new Name;
		GetPlayerName(playerid, Name, sizeof(Name));
		return 1;
	}
Thanks but didn't work, same issue


Re: Problem with a loop - introzen - 21.03.2016

Are you sure your playersName array is updated correctly?


Re: Problem with a loop - Jack_Leslie - 21.03.2016

Quote:
Originally Posted by introzen
Посмотреть сообщение
Are you sure your playersName array is updated correctly?
Yeah I'm positive, it prints correctly until it gets to the data where the players name doesn't match.

So the data is:


And it prints:
Код:
[19:43:30] 0: Jack Leslie, 411
[19:43:30] 1: Jack Leslie, 415
[19:43:30] 2: Jack Leslie, 444
[19:43:30] 4: , 0



Re: Problem with a loop - introzen - 21.03.2016

I have no clue to why this actually would work, since strcmp shouldn't accept the values if one returns null, but try:

pawn Код:
for(new i = 0; i < sizeof(personalVehicleData); i++)
    {
        if(!strcmp(personalVehicleData[i][owner], playersName[playerid], false) && !isnull(personalVehicleData[i][owner]))
        {
            if(personalVehicleData[i][spawned] == 0)
            {
                printf("%d: %s, %d", i, personalVehicleData[i][owner], personalVehicleData[i][modelID]);
                vNumbers ++;
                format(string, sizeof(string), "%s\n%s", string, GetVehicleNameFromModel(personalVehicleData[i][modelID]));
            }
        }
    }
Define isnull() if you haven't.
pawn Код:
#if !defined isnull
    #define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
#endif



Re: Problem with a loop - Jack_Leslie - 21.03.2016

Quote:
Originally Posted by introzen
Посмотреть сообщение
I have no clue to why this actually would work, since strcmp shouldn't accept the values if one returns null, but try:

pawn Код:
for(new i = 0; i < sizeof(personalVehicleData); i++)
    {
        if(!strcmp(personalVehicleData[i][owner], playersName[playerid], false) && !isnull(personalVehicleData[i][owner]))
        {
            if(personalVehicleData[i][spawned] == 0)
            {
                printf("%d: %s, %d", i, personalVehicleData[i][owner], personalVehicleData[i][modelID]);
                vNumbers ++;
                format(string, sizeof(string), "%s\n%s", string, GetVehicleNameFromModel(personalVehicleData[i][modelID]));
            }
        }
    }
Define isnull() if you haven't.
pawn Код:
#if !defined isnull
    #define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
#endif
That worked.. that's extremely weird, wouldn't think you would need to do that.. But it worked.. thank you very much!