home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.os.msdos.programmer
- Path: sparky!uunet!destroyer!ubc-cs!unixg.ubc.ca!penguin!danc
- From: danc@penguin.geog.ubc.ca (Dan Ciarniello)
- Subject: Re: Borland C++ 3.0 scanf()/gets() 'bug'.
- Message-ID: <1992Jul24.202251.6849@unixg.ubc.ca>
- Sender: danc@penguin (Dan Ciarniello)
- Nntp-Posting-Host: penguin.geog.ubc.ca
- Organization: University of British Columbia, Vancouver, B.C., Canada
- References: <1992Jul23.181820.29495@unixg.ubc.ca> <1992Jul24.113144.12779@linus.mitre.org>
- Date: Fri, 24 Jul 1992 20:22:51 GMT
- Lines: 24
-
- To all those who responded to my problem with scanf and gets: Thanks for the help.
-
- The most frequent answer was to use scanf("%s\n", var) instead of scanf("%s", var)
- the theory being that this forces scanf to read (and discard) the newline character
- that's still in the input stream. This seems to agree with the description of how
- scanf works that is in the Borland manuals.
-
- Unfortunately, this doesn't work as advertised. Instead, scanf waits for more input.
- This input is then left in the input stream for the next function that reads the
- input stream. In essence, the problem is the same as before. I have verified this
- with Borland C++, System V cc and AIX xlc.
-
- The solution was provided by Jim Van Zandt: use gets and sscanf.
- (This is also mentioned in the manual under scanf but I missed/ignored this advice
- the first ten times I read it)
- Ie:
-
- gets(strvar);
- sscanf(strvar, "%s", var);
-
- sscanf works similarly to scanf except that it takes input from strvar rather than
- stdin. This combination works with all three of the above mentioned compilers.
-
- Dan.
-