0

I want to store some values in an array and want the array to stop storing values when the user inputs 0 So i have this code :

int numbers[100];
int i = 0;

do

{

        printf("Enter the %d value : ", (i+1) );
        scanf("%d", &numbers[i]);

        i++;
}while (numbers[i] != 0);

But it does not work, i spent like 2 hours on this. Help please Thanks

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 2
    missing semicolon after `i++` – pmg Dec 18 '18 at 17:36
  • 2
    missing semicolon in the first line. Post [MCVE] and a proper description of what is not working. – Eugene Sh. Dec 18 '18 at 17:36
  • 2
    That `i++` means you're repeatedly examining uninitialized data in your do-while condition, btw. Even if you fix the syntax, you're program still invokes *undefined behavior*. You also make no attempt at validating `scanf` actually *worked* (it has a return value for a reason; [check your documentation](https://en.cppreference.com/w/c/io/fscanf)). That's a buried conditional bug just waiting to happen. – WhozCraig Dec 18 '18 at 17:37
  • You write the user value to `numbers[i]` (eg `numbers[2]`), then increment `i` (eg `3`) and test `numbers[i] /* new i! */` (eg `numbers[3]`) which is no longer what the user wrote. – pmg Dec 18 '18 at 17:38
  • Pmg, Eugene, Whozcraig thanks for the comments. 1.Actually i put those semicolons in my code, i just made a mistake while recopying; 2. What was not working that the table does not want to stop storing values when i input 0. –  Dec 18 '18 at 20:43

2 Answers2

2

Expanding on @P__J__'s answer, this works:

int numbers[100];
int i = 0;

do
{
    printf("Enter the %d value : ", (i+1) );
    scanf("%d", &numbers[i]);

}while (numbers[i++] != 0);

There were some missing semicolons, but the major issue is incrementing i before using it in the while condition. i++ returns the original value of i, and then increments it.

tejas
  • 159
  • 2
  • 13
  • 1
    Q: is the "while()" expression [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior)? https://stackoverflow.com/questions/45803172/does-this-code-in-c-fall-into-the-undefined-behavior-category. I *believe* it's OK, because I believe "!=" introduces a sequence point. Q: Have you tried it? Does it work? – paulsm4 Dec 18 '18 at 17:59
  • 1
    afaik, it would be undefined behavior if we had something like `numbers[i++] != i`. But this does not appear to be undefined behavior, and it works. **EDIT:** If you use `numbers[i++] != i` and compile with flags `-Wall -Werror`, you get an unsequenced modification error. But using `numbers[i++] != 0` does not throw an error. – tejas Dec 18 '18 at 18:14
  • 1
    Beautiful - that's what I thought. Thank you for confirming :) – paulsm4 Dec 18 '18 at 18:32
  • It's finally working, thanks tejgop. I just removed the "i++" that was before the condition test and put it in the condition test itself (while number[i++]) –  Dec 18 '18 at 20:43
  • But i don't really understand it, can you explain how the "while ( number[i++] )" work ? –  Dec 18 '18 at 22:01
  • Suppose `i=0`. If you do `number[i]`, it evaluates to `number[0]`. If you do `number[++i]`, it *first* increments it, resulting in `number[1]`. But we did `number[i++]` which first evaluates to `number[0]`, and *then* increments to `i=1`. – tejas Dec 19 '18 at 00:06
1

They work perfect.

But you increase i before checking the condition.

Try }while (numbers[i++] != 0); and remove i++ in the previous line.

0___________
  • 60,014
  • 4
  • 34
  • 74