This is a fabulous question.
The answer is: You cannot force the user to give you good input.
You can only detect it and complain (and quit).
Getting the input is the trick, then. In general, you should consider the following mantra:
→ Every time you ask for input, expect the user to press Enter
This makes life a little more predictable:
Get user input using fgets() (or getline() or any other reasonable function to get a newline-terminated string from the user).
Then try to convert the string to the type of object you desire. If it does not convert, then the user supplied bad input and you can complain and rage quit.
For example, suppose you want a floating-point value from the user:
printf( "What... is the airspeed velocity of an unladen swallow? " );
Get the input:
char s[100];
fgets( s, sizeof(s), stdin );
(Don’t forget to complain if the input is bad — in this case, if the answer doesn’t fit in 99 characters:)
char * nl = strpbrk( s, "\r\n" );
if (!nl) complain_and_quit();
*nl = '\0';
On to step two: attempt to convert the string to the proper type:
char * endp;
double speed = strtod( s, &endp );
If it didn’t work, user’s input is bad. Notice, in particular, how we check to see that the entire string was consumed:
if (*endp) complain_and_quit();
(You may want to add consideration for skipping trailing whitespace, which the user may enter as well.)
Otherwise all is good!
if (20.0 <= speed && speed <= 20.1) // give user benefit of rounding off
printf( "Good job!\n" );
else
printf( "Correct answer is 20.1 mph\n" );
More complex inputs require a bit more vetting, but simply having a function to transform the input string to that type or fail is enough to proceed for any given input type.
As a final note, remember that some types may overlap. For example, an int is also a valid double. If you need to tell them apart, try the integer conversion first, as 3.14 will fail to convert correctly.
Oh, almost forgot: https://interestingengineering.com/science/monty-python-and-the-holy-grail-airspeed-velocity-of-an-unladen-swallow