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

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!seas.smu.edu!pedersen
  3. From: pedersen@seas.smu.edu (Ted Pedersen)
  4. Subject: HELP with scanf/getchar!
  5. Message-ID: <1992Nov9.162547.11163@seas.smu.edu>
  6. Sender: Ted Pedersen
  7. Nntp-Posting-Host: rapid_f.seas.smu.edu
  8. Organization: SMU School Of Engineering and Applied Science
  9. Date: Mon, 9 Nov 1992 16:25:47 GMT
  10. Lines: 71
  11.  
  12.  
  13. The following is an annoying little problem that I just can't figure
  14. out. I'm trying to get the program to prompt me for how many
  15. characters I want to enter and then once I have said how many
  16. characters I want I want the program to ask me to input those
  17. characters.
  18.  
  19. What really happens is that it asks me how many characters I want. I
  20. input that and then my program sits. When I input the appropriate
  21. number of characters the program goes ahead and prints out the message
  22. asking for characters and then runs the program ok. 
  23.  
  24. It seems like this is running out of order. Anyone have any idea as to
  25. what might be going on?
  26.  
  27. Thanks,
  28. Ted Pedersen
  29. --------------------------------------------------------------------
  30. #include <stdio.h>
  31. #include <ctype.h>
  32.  
  33. #define  SIZE  128
  34.  
  35. int     storage[SIZE];
  36.  
  37. void count_char (void);
  38.  
  39.  
  40. void count_char (void) {
  41.     char    character;
  42.     int     count,
  43.             loop,
  44.             output;
  45.  
  46.  
  47.     printf ("Enter number of characters you want : ");
  48.     scanf ("%d\n", &count);
  49.     printf ("Enter your characters :");
  50.     for (loop = 0; loop < count; loop++) {
  51.  
  52.         character = getchar();
  53.         storage[character]++;
  54.  
  55.     }
  56.  
  57.     for (output = 0; output < SIZE; output++) {
  58.         if (storage[output] >= 1) {
  59.             printf ("\n %c (ascii code = %d) occurs %d times",
  60.                     output,
  61.                     output,
  62.                     storage[output]);
  63.         }
  64.     }
  65. }
  66.  
  67. main () {
  68.     count_char ();
  69. }
  70. ------------------------------------------------------------------
  71. %a.out
  72. Enter number of characters you want : 4
  73. abcd                       
  74. Enter your characters :
  75.  a (ascii code = 97) occurs 1 times
  76.  b (ascii code = 98) occurs 1 times
  77.  c (ascii code = 99) occurs 1 times
  78.  d (ascii code = 100) occurs 1 times
  79.  
  80. %exit
  81.  
  82.  
  83.