home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / os / msdos / programm / 8059 < prev    next >
Encoding:
Text File  |  1992-07-25  |  1.6 KB  |  37 lines

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