Here's a sample program showing how to validate user input. It asks for a number between 0 and 10, or -1 to exit. Anything else is considered to be an error.
Hopefully, this may help your understanding.
Code:
#include <stdio.h>
#define MAX 10
int main(void)
{
int num1;
for (;;)
{
printf ("Please enter an int between 0 and %d, or -1 to exit\n", MAX);
if (scanf("%d", &num1) == 1)
{
if (num1 == -1)
break;
if (num1 >= 0 && num1 <= MAX)
printf("You entered %d\n", num1);
else
printf("%d is out of range\n", num1);
}
else
{
printf("That was not a number\n");
while (getchar() != '\n'); /* Eat duff character from buffer */
}
}
return (0);
}