How to validate numeric-integer input in C

I had one of my buddies ask me how to validate input in C today… Turns out the task is kind of daunting. I thought I could get away with the following below:

int status, input;
status = scanf("%d", &input);
while(status!=1){
	printf("Incorrect number... please try again: ");
	status = scanf("%d", &input);
}

but the problem is you receive an infinite loop. The reason behind this is when you hit return\enter on your keyboard, a newline character is passed in as input. These extra hidden characters are what is messing with your input and spawning the infinite while loop. Luckily, I was able to parse through the extra characters one-by-one and get something working.

Hopefully this helps someone else!

#include<stdio.h>
int main(void){
	// input	user input -- hopefully a number
	// temp		used to collect garbage characters
	// status	did the user enter a number?
	int input, temp, status;

	printf("Please enter a number: ");
	status = scanf("%d", &input);
	while(status!=1){
		while((temp=getchar()) != EOF && temp != '\n');
		printf("Invalid input... please enter a number: ");
		status = scanf("%d", &input);
	}

	printf("Your number is %d\n",input);
	return 0;
}

10 thoughts on “How to validate numeric-integer input in C

  1. Chuck

    Instead of checking for \n what you could do is read in that character and not use it.


    #include
    int main(void){
    // input user input -- hopefully a number
    // temp used to collect garbage characters
    // status did the user enter a number?
    int input, temp, status;

    printf("Please enter a number: ");
    status = scanf("%d", &input);
    temp = scanf("%d", &input);
    while(status!=1){
    printf("Invalid input... please enter a number: ");
    status = scanf("%d", &input);
    temp = scanf("%d", &input);
    }

    printf("Your number is %d\n",input);
    return 0;
    }

    Reply
      1. Chuck

        Revised working code. I guess the second scanf has to read a char to work. I was on a computer without a C compiler so I was testing it on ideone.com. Also I don’t know why you assign something to status with the scanf when you should just &status for assignment.


        #include
        int main(void){
        // input user input -- hopefully a number
        // temp used to collect garbage characters
        // status did the user enter a number?
        int input, temp, status;
        printf("Please enter a number: ");
        scanf("%d", &status);
        scanf("%c", &temp);
        while(status!=1){
        printf("Invalid input... please enter a number: ");
        scanf("%d", &status);
        scanf("%c", &temp);
        }
        printf("Your number is %d\n",status);
        return 0;
        }

        Reply
  2. Arturo Ibarra

    Hi! I implemented your code into my program but it didn’t work! The program just went through to the second input. What could have been the problem?

    Reply
    1. Jack Post author

      Hi Arturo,

      That’s weird, the loop should pull the first value and garbage collect the return. What value does get assigned to the variable?

      Did you give Chuck’s code in his follow up a shot?
      Jack

      Reply
  3. Clint

    This worked perfectly for me, resolved the infinite loop with two lines of code.
    Thanks for posting it, Jack, very helpful.

    Reply
  4. David

    Thank you Jack! This has been a long time struggle for me in college now I never have to worry about this annoying problem again! Again THANK YOU!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.