I'm struggling to come up with a solution for a problem that I set myself to complete, I have tried multiple different ways but I can't seem to get it to behave.
What I'm attempting to do is handle when non-numeric characters are input by a user in scanf. The end goal is to have the program ask the user for a value from a series, and keep asking until a float or int is entered, then finish the series.
Seeing as it's a short program, ill post the whole thing. Any help is much appreciated.
int main(void) {
int numberOfValues, ctr, inputValidation, avg;
float sum = 0;
printf("\nHow many values are you averaging?: ");
scanf("%d", &numberOfValues);
float inputValue[numberOfValues];
printf("\n");
for (ctr = 0; ctr < numberOfValues; ctr = ctr + 1) {
printf("\tPlease enter value %d: ", ctr + 1);
inputValidation = scanf("%f", &inputValue[ctr]);
if (inputValidation != 1) {
printf("\tPlease enter value %d again: ", ctr + 1);
scanf("%f", &inputValue[ctr]);
} else {
sum += inputValue[ctr];
}
}
avg = sum / numberOfValues;
printf("\nYour average is: %g", avg);
return 0;
}