SA-MP Forums Archive
While loop problem - 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: While loop problem (/showthread.php?tid=388344)



While loop problem - Ambokile - 28.10.2012

Hello, thanks for reading this.
I'm having a few problems with a while loop to load cars in my script. Here is my code:
pawn Код:
forward loadCars_query();
public loadCars_query()
{
    new rows, fields;
    new i = -1;
    cache_get_data(rows, fields);
    if(rows)
    {
        while(i < rows)
        {
            i++;
            new temp[4], id;
            printf("i | rows : %d | %d", i, rows);
            cache_get_field_content(i, "ID", temp), id = strval(temp);
        }
    }
}
As you can see, i++ should be increased with every iteration. However, the log prints this out (the server hangs, too):
Quote:

i | rows : 0 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2

I'm not sure what I'm doing wrong, I hope one of you can help me.


Re: While loop problem - ryansheilds - 28.10.2012

I'm not sure what the problem is but try using a for loop?

pawn Код:
forward loadCars_query();
public loadCars_query()
{
    new rows, fields;
    cache_get_data(rows, fields);
    if(rows)
    {
        for(new i = -1; i < rows; i++)
        {
            new temp[4], id;
            printf("i | rows : %d | %d", i, rows);
            cache_get_field_content(i, "ID", temp), id = strval(temp);
        }
    }
}
I believe a for loop is faster anyway.


Re: While loop problem - Abhishek. - 28.10.2012

well while loop is faster and do PWN have while loop OMG! i thought it was only in BlueJ(JAVA)
EDIT:and i too think that the upper for loop gona work because i do think that loop's dont support negative value's coz my teacher Once Cutted my whole program due to it in my exams ;( i lost my 15 marks due to it could be your while loop problem too


Re: While loop problem - Ambokile - 28.10.2012

I only used -1 to see if placing i++ at the beginning would make the code work. I'll try that for loop in a second, thanks.


Re: While loop problem - Ambokile - 28.10.2012

I tried this:
pawn Код:
forward loadCars_query();
public loadCars_query()
{
    new rows, fields;
    cache_get_data(rows, fields);
    if(rows)
    {
        for(new i = 0; i < rows; i++)
        {
            new temp[4], id;
            printf("i | rows : %d | %d", i, rows);
            cache_get_field_content(i, "ID", temp), id = strval(temp);
        }
    }
}
And just get the same output.


Re: While loop problem - Ambokile - 28.10.2012

The cache_get_field_content is from BlueG's SQL plugin (see here: https://sampforum.blast.hk/showthread.php?tid=337810)

However, I used cache_get_row rather than cache_get_field_content (which should both do the same thing, but I'm not sure why they didn't, and it worked fine. Here is the code I settled with:
pawn Код:
forward loadCars_query();
public loadCars_query()
{
    new
        rows,
        fields;
       
    cache_get_data(rows, fields);
   
    if(rows)
    {
        new
            temp[5],
            i,
            id;
           
        while(i < rows)
        {
            cache_get_row(i, 0, temp);
            id = strval(temp);
            printf("i | rows : %d | %d", id, i, rows);
            i++;
        }
    }
}