Click to See Complete Forum and Search --> : Errror checking ( please help my pitiful self)


tnordloh
03-23-2001, 09:37 AM
Ok, here's my dilemma; I have a program that asks the user for an integer. The problem is that if the user types something invalid (such as a letter) it just uses the last integer value entered. What would be a good way to error-check this?

Here's my current solution; it dumps all characters out of the command line after getting the first integer, and if the integer is 0, it assumes that is an invalid number.

Obviously, I'd like to change this so that I can use any integer. Any suggestions?


key=0;
while(key == 0)
{
printf("please enter the number to search for: ");
scanf("%d",&key);
while(trash=getchar()!='\n');
}


Thanks for the help anyone, I'm just drawing a blank on this one...

tnordloh
03-23-2001, 09:54 AM
I'm a dummy; I just have to check the return value of scanf. rtfm would be wise for me BEFORE I post...