I need to write a program in pure C. I wish to fill an array with user inputted floats and my function at the moment looks like this:
int fillWithCustom(float *array, int size) {
float customNumber;
for (i = 0; i < size; i++)
for (j = 0; j < size; j++) {
printf("\n Enter [%d][%d] element: ", i , j);
scanf("%f", &customNumber);
*(array+i*size+j) = customNumber;
}
return 1;
}
But when I enter wrong number or char, iteration continues to an end…(Ex. I enter “a” as first element, then both for cycles iterate without scanf’s and array is filled with 0‘s.
Don;t use
scanf()for user input. It was written to be used with formatted data. User input and formatted data are as different as night from day.Use
fgets()andstrtod().