mxmora@unix.SRI.COM (Matt Mora) (04/12/91)
To all you C guru's out there HELP!
I must be missing something totally obvious but the code below
is not working as expected. :-( scanf is supposed to return the
number of successful conversions correct? In the code below
whether I type in a number or a string of characters, test always equals
one.
Shouldn't test become zero when a non numeric input is typed in?
This happens in think C 4.0.2
while ((test=scanf("%d",&thenumber))!=EOF)
{
printf("The Number %d Test %d \n",thenumber,test);
printf("Enter a number ");
thenumber=0;
fflush(stdin);
}
Also when I compiled this on my unix machine after I typed in a string
of characters it went into an endless loop not allowing me to enter in
another value. It just kept printing "The Number ..."
What the heck am I doing wrong.
Thanks
Matt
--
___________________________________________________________
Matthew Mora | my Mac Matt_Mora@sri.com
SRI International | my unix mxmora@unix.sri.com
___________________________________________________________
gort@cup.portal.com (george d harrington) (04/12/91)
matt mora (mxmora@unix.sri.com) writes: >I must be missing something totally obvious but the code below >is not working as expected. :-( scanf is supposed to return the >number of successful conversions correct? In the code below >whether I type in a number or a string of characters, test always equals >one. > >Shouldn't test become zero when a non numeric input is typed in? > >This happens in think C 4.0.2 > >while ((test=scanf("%d",&thenumber))!=EOF) > { > printf("The Number %d Test %d \n",thenumber,test); > printf("Enter a number "); > thenumber=0; > fflush(stdin); > } > >Also when I compiled this on my unix machine after I typed in a string >of characters it went into an endless loop not allowing me to enter in >another value. It just kept printing "The Number ..." Yes, scanf should return zero when a non-number is typed in. But this won't get you out of the loop, since EOF is not zero. It should just keep looping until you hit ctrl-d (under unix). This is in fact what the code does compiled on the standard cc under sunOS 4.0.3. Didn't try it under think c, but it should behave the same. If what you really want to do is exit the loop when you enter a non- numeric, change it to while ( scanf("%d",theNumber) != 1) Actually, gets() seems like a more useful function, allowing the user to enter anything, then you can use sscanf() and the fcns from ctypes.h like isalpha(), etc. to handle whatever strange things the user enters. If you didn't make a typo when retyping the code onto your unix box, the only thing i can think of is putting an fflush(stdout) in your loop. Certain unices seem to want an inordinate number of fflushes to keep them happy (CTIX from convergent comes to mind.) Just out of curiosity, what unix are you using? gort@cup.portal.com
lindahl@violet.berkeley.edu (Ken Lindahl 642-0866) (04/16/91)
In article <41210@cup.portal.com> gort@cup.portal.com (george d harrington) writes: >matt mora (mxmora@unix.sri.com) writes: ... >>while ((test=scanf("%d",&thenumber))!=EOF) ... >If what you really want to do is exit the loop when you enter a non- >numeric, change it to > while ( scanf("%d",theNumber) != 1) Actually, to exit the loop when a non-number is scanf'ed: while ( scanf("%d",&theNumber) == 1) ^^ Ken Lindahl lindahl@violet.berkeley.edu Advanced Technology Planning, Information Systems and Technology University of California at Berkeley