Re: Help me stay away from visual C++ :)
On 26 Jul, Moses Leslie wrote:
| I'm taking an intro C class, and of course everything there is run on win95.
| For a project we're doing now, I need to be able to tell if a scanf("%d",&x)
| actually gets an int or not, but scanf seems to freak out if it gets anything
| but an int. For example:
|
| int test;
| for(;;)
| {
| printf("Status is %d\n",scanf("%d",&test));
| fflush(stdin);
| }
|
| prints out "Status is 1" if it gets an int, but freaks out and keeps printing
| "Status is 0" over and over if you give it a char. The same snippet works fine
| under visual C++. Is this something that's (most likely) broken in vc++, or
| perhaps (less likely) broken in glibc 2.1? All I have to test it on is a
| potato box, so I don't know if other versions of gcc have the same problem.
|
| Pointers to faqs or relevant docs are appreciated, I've spent an entire day
| poking around in various gcc things (I hate info :)) but with no luck.
|
| Thanks,
| Moses
|
| --
| Moses Leslie <marmoset@l8r.net>
|
|
Firstly, scanf is evil. Don't use it for user input! If you want to see
if input is an integer or not, you need to read it as a string first
with, may I suggest fgets(), or simply getchar() for a single
character. Then use isdigit() to see if it is a digit. Also,
fflush(stdin) is guaranteed to give "undefined behavior", which is
certainly part of your problem. I highly recommend comp.lang.c as a
place to get expert advice on ANSI/ISO C (don't ask about OS specific
stuff! and please read the C FAQ).
--
Eric G. Miller
Powered by the POTATO (http://www.debian.org)!
Reply to: