Hmm.. maybe this will be helpful.
Program with problems...
Code:
#include <stdio.h>
int main ()
{
int i;
printf ("Type in numbers, foo!\nType in 0 to quit!\n");
while ()
{
scanf ("%d", &i);
if (i == 0) break;
printf ("You typed in %d\n", i);
}
return 0;
}
And the program without the same problems...
Code:
#include <stdio.h>
int main ()
{
int i, j;
printf ("Type in numbers, foo!\nType in 0 to quit!\n");
while ()
{
j = scanf ("%d", &i);
if (j != 1)
{
printf ("That ain't a number, get outta my program!\n");
break;
}
if (i == 0) break;
printf ("You typed in %d\n", i);
}
return 0;
}
Just trying to take advantage of the fact that scanf returns the number of arguments scanned in, or EOF if EOF is encountered. So basically, in that call of scanf, it is going to either return 1, which means it succesfully scanned something in, a 0 which means it couldn't read any arguments (because the first one wasn't an int), or EOF, which shouldn't matter unless you use a file as stdin.
As usuall, I'm not -sure- that the programs 'll work, but I like to inagine they do.