Someone please help me out here.
I'm trying to fill an int array with values from stdin.
The user enters values one per line. When anything other than a number is
entered,the while loop should stop, leaving the remainder of the array
untouched.
Instead, here is how it works.
If very first entered value is not a number, the program exits the loop
just like I want it to. But, if not a number value has been entered second or
third and so on.., the loop goes into infinity. Without even stopping at the 'scanf'!!! And the val variable remains the last numerical value that has been entered.
What is going on? Please help.
1 2 3 4 5 6 7 8
|
int val=1;
int i=0;
scanf("%i", &val);
while(val != 0){
arr[i] = val;
scanf("%i",val);
i++;
}
|