home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19544 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.0 KB  |  75 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!agate!stanford.edu!kronos.arc.nasa.gov!butch!netcomsv!netcomsv!ulogic!hartman
  2. From: hartman@ulogic.UUCP (Richard M. Hartman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: ** Ack! Help again! **
  5. Message-ID: <841@ulogic.UUCP>
  6. Date: 11 Jan 93 17:18:34 GMT
  7. References: <kaNTwB1w165w@tradent.wimsey.bc.ca> <dschmitz.726261452@pv0321.vincent.iastate.edu>
  8. Organization: negligable
  9. Lines: 64
  10.  
  11. In article <dschmitz.726261452@pv0321.vincent.iastate.edu> dschmitz@iastate.edu (Daniel C Schmitz) writes:
  12. >In <kaNTwB1w165w@tradent.wimsey.bc.ca> lord@tradent.wimsey.bc.ca (Jason Cooper) writes:
  13. >
  14. >>time I use scanf or gets, and I ask for more than one input of a string 
  15. >>in a row, like this:
  16. >>gets(string1)
  17. >>gets(string2)
  18. >>or however that works (the point being they are seperate lines), C will 
  19. >>skip some of the inputs for apparently no reason at all. 
  20. >
  21. >I have often had problems with scanf followed by a gets.  The first gets
  22. >that follows any series scanf's always returns an empty string.
  23. >A hack that has worked for me on some systems is to put a dummy gets between
  24. >scanf's and gets's.
  25. >
  26. >scanf(...)
  27. >scanf(...)
  28. >gets(..)    /* this is a dummy, to fix problems that I've experienced */
  29. >get(...)    /* legitimate gets, use the results */
  30. >scanf(...)
  31. >gets(...)    /* dummy */
  32. >gets(...)    /* useful value */
  33. >
  34. >dschmitz
  35. >dschmitz@iastate.edu
  36.  
  37. There should be no need for such a hack.
  38. In your scanf(), does the format string contain a "\n"?
  39. Try this:
  40.  
  41.     /* test.c */
  42.     #include <stdio.h>
  43.     main()
  44.     { 
  45.     int i; char buf[81];
  46.  
  47.     fflush(stdin);
  48.     scanf("%d", &i);
  49.     gets(buf);
  50.     printf("[%d], [%s]\n", i, buf);
  51.  
  52.     fflush(stdin);
  53.     scanf("%d\n", &i);
  54.     gets(buf);
  55.     printf("[%d], [%s]\n", i, buf);
  56.     }
  57.  
  58. log of run:
  59.  
  60.     $ ./test
  61.     23
  62.     [23], []
  63.     132
  64.     fsdljk
  65.     [132], [fsdljk]
  66.     $
  67.  
  68.  
  69.         -Richard Hartman
  70.         hartman@ulogic.COM
  71.  
  72. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  73. "There are no problems, only opportunities!"
  74. "What we have here is an insurmountable opportunity..."
  75.