0

I'm trying to ensure that the user has definitely entered an integer. However no matter what I type in, it doesn't seem to work.

Sometimes the scanf is completely ignored and the loop just prints out everything loads of times.

When I checked what the input was that the code took in, it was wrong as well.

Any ideas or help would be appreciated!

while (square_size == -1) {
    square_size = get_input_size();
}

int get_input_size(void) {

   int size;
   printf("What size word square would you like to create?  ");
   scanf("%d", &size);
   if (isdigit(size)) {
       printf("VALID %d\n", size);
       return size;
   }
   printf("ERROR: invalid input\n");
   return -1;
}
numberjak
  • 1,065
  • 4
  • 13
  • 28
  • 3
    If invalid input is entered, it remains in the input stream and the next call to `scanf()` reads it again. You need to clear the input stream after bad input is detected. Also, check the value returned from `scanf()`; if non-numeric input is entered, nothing is stored in `size`, which then holds an _indeterminate_ value, leading to UB in `isdigit(size)`. – ad absurdum Dec 17 '17 at 15:02
  • @numberjak I think that maybe [This is what you are looking for](https://stackoverflow.com/questions/40138287/re-prompting-a-user-until-he-she-enters-a-positive-integer-value-greater-than-1/40139212#40139212) – Michi Dec 17 '17 at 15:08

0 Answers0