home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / programm / 3124 < prev    next >
Encoding:
Text File  |  1992-11-11  |  3.1 KB  |  92 lines

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!caen!batcomputer!munnari.oz.au!bruce.cs.monash.edu.au!monu6!giaeb!s1110238
  3. From: s1110238@giaeb.cc.monash.edu.au (Lee Hollingworth)
  4. Subject: Re: HELP with scanf/getchar!
  5. Message-ID: <s1110238.721534396@giaeb>
  6. Sender: news@monu6.cc.monash.edu.au (Usenet system)
  7. Organization: Monash University, Melb., Australia.
  8. References: <1992Nov9.162547.11163@seas.smu.edu> <1992Nov9.205548.5984@bcrka451.bnr.ca>
  9. Date: Thu, 12 Nov 1992 02:13:16 GMT
  10. Lines: 80
  11.  
  12. sjm@bcrki65.bnr.ca (Stuart MacMartin) writes:
  13.  
  14. >In article <1992Nov9.162547.11163@seas.smu.edu> pedersen@seas.smu.edu (Ted Pedersen) writes:
  15. >>
  16. >>The following is an annoying little problem that I just can't figure
  17. >>out. I'm trying to get the program to prompt me for how many
  18. >>characters I want to enter and then once I have said how many
  19. >>characters I want I want the program to ask me to input those
  20. >>characters.
  21. >>
  22. >>What really happens is that it asks me how many characters I want. I
  23. >>input that and then my program sits. When I input the appropriate
  24. >>number of characters the program goes ahead and prints out the message
  25. >>asking for characters and then runs the program ok.
  26. >>
  27. >>It seems like this is running out of order. Anyone have any idea as to
  28. >>what might be going on?
  29. >>
  30. >>Thanks,
  31. >>Ted Pedersen
  32. >>--------------------------------------------------------------------
  33. >You need to flush your buffers.  This is normally done with a '\n', but
  34. >if you don't have that, you need to explicitly call fflush:
  35.  
  36. >>    printf ("Enter number of characters you want : ");
  37. >     fflush(stdout);   /* <=== THIS LINE WAS MISSING */
  38.  
  39. Shouldn't that be "fflush(stdin)" ?
  40. However if you look at the ANSI standard library it says that using fflush
  41. "on an input stream, the effect is undefined", therefore a more portable
  42. method is to define a macro...
  43.  
  44. #define myflush()   while (getchar() != '\n)
  45.  
  46. Of course the macro has to be used *after* user input not before.
  47.  
  48. However from the little bit of code left in this posting I would think
  49. you are trying to do unbuffered input which is not possible with standard
  50. C library functions.  (Unless of course you wish to let the user add as many
  51. characters as they like but only read 'x' of them into your array).
  52.  
  53. #include <stdio.h>
  54.  
  55. #define myflush()   while (getchar() != '\n')
  56.  
  57. int main(void)
  58. {
  59.     int count;
  60.     char word[81];
  61.     int i;
  62.  
  63.     printf("Enter number of characters: ");
  64.     if (scanf("%d", &count) == 0) {
  65.         puts("Non-integer entered");
  66.         return (1);
  67.     }
  68.     myflush();
  69.     if (count > 80) {
  70.         printf("Array dimensions exceeded\n");
  71.         return (1);
  72.     }
  73.     printf("Enter %d characters: ", count);
  74.     for (i = 0; i < count; ++i) {
  75.         if((word[i] = getchar()) == '\n') {
  76.             /* print warning or something */
  77.             break;
  78.         }
  79.         word[i+1] = 0;  /* avoid junk in memory for newline test */
  80.     }
  81.     if (word[i] != '\n') {
  82.         myflush();
  83.     }
  84.     word[i] = '\0';
  85.     printf("%s\n", word);
  86.     return (0);
  87. }
  88.  
  89.  
  90. Lee Hollingworth
  91. s1110238@giaeb.cc.monash.edu.au
  92.