Problem with a loop
#1

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

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;
	}
Reply
#3

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

Are you sure your playersName array is updated correctly?
Reply
#5

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

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)