i'm doing calculation for result. user will input 3 type of result that is pass, fail & invalid. I need to have 1 more checking that is when user input result other than integer value it will be count as wrong input. By using fputs function, how can i ask user to input integer data only ? instead of stopping, program will ask user to input again until user input the correct data.
#include <stdio.h>
int main(void) {
int test_result;
int pass;
int fail;
int invalid;
for (int trainee_total; trainee_total < 6; trainee_total++) {
printf("Enter Result (Pass=1 Fail=2) :");
if (scanf("%d", &test_result) != 1) {
fputs("Wrong input\n", stderr);
return 1;
}
if (test_result == 1) {
pass = pass + 1;
} else if (test_result == 2) {
fail = fail + 1;
} else {
invalid = invalid + 1;
}
}
printf("The number of participants that passed is : %d\n", pass);
printf("The number of participants that failed is : %d\n", fail);
printf("Invalid input is : %d\n", invalid);
}
You probably want something like this:
GetIntFromUserfunction is inspired from this answer.(mandatory)are needed so the program is correct, The comments with(better style)just show that it is a better style.GetIntFromUserfunction. If e.g.11 22 33is entered the program output looks kind of strange.