0

Probably a simple answer to this, but I am relatively new to C. I have a loop that validates that a user has entered an integer value within certain parameters. It works fine, except when a user inputs a character. I thought my code checked for this already, but I was wrong.

My code at the moment:

    while(1) {
                printf("Enter Recommended Shot Doses (1-255): ");
                if(scanf("%d", &shotDoses) == 1 &&
                    shotDoses >=1 && shotDoses <=255) break;
                
                printf("\033[1;31mPlease Enter a Valid Number.\033[0m\n");
            }

I get an infinite loop if you accidentally input a character instead of a number. Any help?

Thanks

  • The scanf() actually returns the number of successful conversions from the input buffer, if the conversion fails it returns 0 . If you input a char ,it can not be converted and is not removed from buffer, thus results in the endless loop. You need to flush the input buffer – Harshit Sharma Feb 06 '22 at 21:00
  • `scanf` does not relove invalid characters. Quick fix: after error, read chars until newline. Better solution: read entire line (maybe with `fgets`, maybe with POSIX `getline`, maybe your own line reading function), _then_ parse the read line. – hyde Feb 06 '22 at 21:01
  • https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm – Andrew Truckle Feb 06 '22 at 21:01
  • The link gives explkanation of return value and sample code. – Andrew Truckle Feb 06 '22 at 21:02
  • 1
    @AndrewTruckle Please double check that you link provides an answer to the endless loop problem. – Yunnosch Feb 06 '22 at 21:40

1 Answers1

0

Read a line to the buff, then scan it.

#define MAXLINELENGHT 100

    while(1) {
                char buff[MAXLINELENGHT];
                printf("Enter Recommended Shot Doses (1-255): ");
                fgets(buff, sizeof(buff), stdin);
                if(sscanf(buff, "%d", &shotDoses) == 1 &&
                    shotDoses >=1 && shotDoses <=255) break;
                
                printf("\033[1;31mPlease Enter a Valid Number.\033[0m\n");
            }
S3DEV
  • 8,768
  • 3
  • 31
  • 42
0___________
  • 60,014
  • 4
  • 34
  • 74
  • (a) There is no standard `snscanf` function. (b) This code will incorrectly parse a numeral that is split by the limit passed to `fgets`. – Eric Postpischil Feb 06 '22 at 23:54
  • @EricPostpischil (b) - if someone will enter first 95 spaces then 8 digits number then yes. But I do not think that OP is going to do idiot prone system. But anyway if user is an idiot then he will be asked again. Not a very big deal. – 0___________ Feb 07 '22 at 08:04
  • No, when the user enters 95 spaces then eight digits, they will not be asked again. The first four will be taken as the numeral, and the program will go on, where the remaining digits will cause a problem. And there is no reason for this. It is not hard to write correct code for this, so students should not be given incorrect code. – Eric Postpischil Feb 07 '22 at 11:23