Better Input Checking

Here is a good way to check for valid input in C. First, you try to get the input. If the scanf can't correctly parse the input, then you have to throw away the line and try again. If the input could be read, but has a bad value, you also have to try again.

/* the good_input variable is used to keep track of whether or not */
/* the user's input is okay.  result is used for the scanf success result*/
int good_input, result;
/* we need a character variable for scrap. */
char c;

/* ... */

good_input = 0;
/* keep going until we get good input */
while ( good_input != 1 ) {
  /* try to get the input */
  printf("Give me the input:\n");
  result = scanf("%i", &var);

  /* hope for the best :-)  */
  good_input = 1;

  /* if the input could not be read, throw it away and try again. */
  if ( result != 1 ) {
    /* if scanf couldn't deal with the input, it will still be sitting */
    /* there, waiting to be read.  If we try to read the same stuff again, */
    /* it will fail again and again, sending into an infinite loop.  These */
    /* lines will eat away the rest of the line so it doesn't bug us next */
    /* time around. */
    do {
      scanf("%c", &c);
    } while ( c != '\n' );
    good_input = 0;
  }

  /* if the input didn't make sense, try again. */
  if ( /* var is not in the correct range, or whatever */ ) {
    /* if the input was out of range, we don't have to fix anything, just */
    /* ask again. */
    good_input = 0;
  }
}

/* do it again for more input? */

If you're reading two variables at once, the condition should be result != 2, and result != 3 for three variables, etc.