i have this function in my project:
void inputIntArray(int array[], int size){
j = size;
for(i=0;i<j;i++){
printf("* Enter number #%d out of %d: ",(i+1),j);
scanf("%d",&k);
array[i] = k;
}
}
It's supposed to fill an array, and works fine when integers are input, when something which is not an integer is being inputted it will just fill the remaining cells in the array with the last integer inputted and skip the whole project while only printing everything remaining...
Anyone knows what causes this error? Thanks!
EDIT: After changing to:
void inputIntArray(int array[], int size){
int i = 0,j = 0,k = 0;
j = size;
for(i=0;i<j;i++){
printf("* Enter number #%d out of %d: ",(i+1),j);
if(scanf("%d",&k) != 1){
break;
}
array[i] = k;
}
}
I'm still experiencing the same error.....
scanfis the number of successfully read inputs. If you check that, you can act appropriately. You're expecting 1, if you don't get that, do something about it.