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; }
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;
}
Chuck, unfortunately, your code does not work 🙁 If you enter in text, you will notice your loop runs infinitely.
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;
}
excellent....I was trying with something similar for hours. thanks
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?
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
This worked perfectly for me, resolved the infinite loop with two lines of code.
Thanks for posting it, Jack, very helpful.
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!
What does it mean when you say EOF in this line code? while((temp=getchar()) != EOF && temp != '\n');
End of File
Thanks for help, it worked for me
Hi, Jack, thank you very - very much for this code. I've been searching during several days for the right input resolving, and you realy saved me, man. Big thanks!
It was very helpful but i'm looking for a way to test for data type please
Hello,
I have been struggleling with the same issue for weeks, I am so happy I found this that I thought that I should write a few lines not only to thank the author for sharing but also to let others know that this is really working.
Thank you
Really useful, it works properly. I spend a lot of time trying to solve this problem. You saved me a lot of time! thanks!
The above discussion was helpful. I was able to do my homework.
This allows only positive values and asks for user input again if the negative value or character values are input.
#include
#include
//global variable defined
int n;
//taking and validating user input
int userinput() {
int check;
int cha;
int charr;
printf("Enter number of elements to put in array: ");
check = scanf("%d", &n);
charr = scanf("%c", &cha);
while ((check != 1) || (n < 0)) {
printf("Your input is incorrect\n");
printf("Please enter an postive integer: ");
check = scanf("%d", &n);
charr = scanf("%c", &cha);
};
return (n);
};