Input validation (C)

Soldato
Joined
19 Oct 2002
Posts
2,707
Location
UK
I have to take a number of inputs which must be positive and not exceed a short int, I have been told 'the user must be able to bash their hands on the keyboard and throw the computer out of the window and your program must not break' :o I did the following...

Code:
	do {

		printf("Enter value for x (0-32768): ");
		scanf("%d", &x);

	} while( (x < 0)||(x > 32768) );

But when I put anything in that isn't a number it goes into an infinite loop and I'm not sure why or how to fix it.
 
The point of the loop is to keep asking for the input until an input which meets the requirements is given. If i initialise x as 0 before or change the conditions it won't work. I think I will have to come up with another way to validate.
 
Back
Top Bottom