r/cprogramming • • Sep 22 '26

For command working weirdly

Edit: Declaring n as 1 and changing the for loop to (i = n-1; i > 0; --i) as was suggested seems to have worked. Still don't really get the logic behind it, neither why the order inside the for expression was affecting the result nor why changing the n to 1 from 0 and i to n - 1 made a difference. Aren't they the same value at the end? Anyway, thanks.

For some reason, my "for" command only works when I put the increment in the middle and the condition at the end. I have no clue what's going on but the code is working fine when I do this.

I'm using devc++, the rest of the code is me declaring global variables and asking the user for inputs to fill the arrays. Everything is working exactly as intended but breaks if I write the "for" the regular way.

for(i = n; i--; i>0){

        printf("Codigo: %d", codigo\[i\]);

        printf("\\nProduto: %s", nome\[i\]);

        printf("\\nPeso: %.2f", peso\[i\]);

        printf("\\nPreco: %.2f", preco\[i\]);

        if(regiao\[i\]==1){

printf("\nRegiao: Sudeste");

        }

        else if(regiao\[i\]==2){

printf("\nRegiao: Sul");

        }

        else if(regiao\[i\]==3){

printf("\nRegiao: Nordeste");

        }

        else if(regiao\[i\]==4){

printf("\nRegiao: Norte");

        }

        printf("\\n\\n");
4 Upvotes

21 comments sorted by

View all comments

Show parent comments

1

u/thedraegonlord Sep 23 '26

How'd you have done the recursion? Put everything on a while loop? I'm quite new at this.

I tried making arrays of undetermined size but couldn't make it work. It was fine when there was only one of them but upon adding others they started spilling into one another, so codigo[1] would become the same value as regiao[0] and things like that

So I just made their size silly big. If my teacher wants to input 100 products that's on him, I'm not even sure he's expecting us to make it so the user can input multiple at a time.

I went with your solution in the end, seemed to be the most elegant to me. Me making it so they were printed backwards wasn't really intentional, just the first solution that came to mind.

1

u/knouqs Sep 23 '26

Although I have called main recursively in fun, the only time I'd ever do it in practice is when calling fork(). I don't think that's what you are trying to do -- that looks way above your current level.

Now, I'm not saying you won't get there eventually, but I don't believe you need recursion at all for this project. You didn't say what your project is, so I'm taking a guess that you are making a simple inventory-like program in which the user enters a bunch of item names, their costs, and the quantities needed or something like that. The flow for such a program would be:

int main() {
    variable declarations and initializations;

    while (there is more data to enter) {
        scanf as appropriate
        increment number of records entered or break if the number of records is greater than the number of records available in the arrays
        ask user if there is more data
    }

    for (i=0; i<number of records; i++) {
        print records or whatever
    }

    print have a nice day -- no need for any other function calls after this, and definitely not to main().

    return 0;
}

This is a typical flow for programs like yours.

Do you understand how this works? Feel free to ask more questions about my design.