I am trying to get input into an array, until a user enters -1 to exit the input mode.
Something strange is happening in this block of code when a -1 is entered but only after at least two values have been entered.
#define ARRAYSIZE 100
int input[ARRAYSIZE];
int i=0;
do {
printf("Enter data #%d or -1 to exit: ", i);
scanf("%d", &input[i]);
} while(input[i++] != -1 && i<ARRAYSIZE)
The code will just hang indefinitely. I have compiled and run on two separate architectures, but the problem doesn’t show up when it is run in gdb.
Inserting print statements revealed that the code is indeed hanging at the scanf statement.
Does anyone have any idea what might be causing this?
you were never checking the value of the data you saved (it was check the next uninitialized element), do while is a better fit