2

Having trouble getting an actual count, but I'm not seeing what I'm doing wrong.

I type in a word, hit enter, then nothing happens and it keeps running.

int main(void)
{    
  double nc;

  for (nc = 0; getchar() != EOF; ++nc)
      ;
  printf ("%.0f\n", nc);
}
NineBerry
  • 26,306
  • 3
  • 62
  • 93
Wayne
  • 23
  • 2
  • 6
    Why on earth do you use a floating-point number for counting? – DYZ Mar 08 '17 at 00:18
  • I'm just following this book, the c programming language – Wayne Mar 08 '17 at 00:25
  • 2
    I am not suggesting that you should toss that book away ASAP, but you probably should, anyway. Floating-point numbers are imprecise and should never be used for counting. Use integer numbers. As for your question, you must press ^D to close input. – DYZ Mar 08 '17 at 00:27
  • 1
    You have to end the input with either ENTER followed by ctrl-D, or two ctrl-d's back to back. this is similar to an earlier question. Read the answers there for some background. http://stackoverflow.com/questions/27183865/getchar-eof – ScottK Mar 08 '17 at 00:29
  • 1
    Someone suggested the book to me as I have never coded before, and I wanted to start with some basic stuff – Wayne Mar 08 '17 at 00:29
  • Is there a book anyone suggests for someone who has barely any knowledge about all this – Wayne Mar 08 '17 at 00:34
  • 1
    If you want code to stop at the end of a _line_, detect `'\n'`. `int ch; for (nc = 0; ((ch = getchar()) != EOF && ch != '\n'); ++nc) ;` – chux - Reinstate Monica Mar 08 '17 at 00:38
  • @Wayne If you mean the book by Kernighan & Ritchie, it's a good book. – Bemmu Mar 08 '17 at 00:46

2 Answers2

3

When reading from an interactive console input, getchar() will not return EOF just because the user stops typing or presses return, it will if necessary wait for the user to enter something new on the keyboard. So, the for-loop is not terminated.

You have to use a special key combination (dependent on the operating system used) to signal end-of-file or check for some other input to terminate the loop (like end-of-line, for example)

NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • This is not true. If you press ^D (or a similar character in a Mac terminal), an `EOF` marker is generated, and `getchar()` returns `EOF`. – DYZ Mar 08 '17 at 00:30
  • @DYZ answer clarified – NineBerry Mar 08 '17 at 00:39
  • It would be better if you mention what those special key combinations to trigger `EOF` are, at least for linux and windows, in your answer. – Spikatrix Mar 08 '17 at 01:14
1

As noted, EOF will not be true until ^D is entered. Terminate on that, but also check for the newline character. Check this older Question for some good background on this here: getchar() != EOF

Here is the program written using ints and checking for newline or EOF

// Customize AT_END Macro to return true based on some condition
#define AT_END(ch) (((ch)=='\n') || ((ch)==EOF)) 

int main(void)
{    
  int  inpChar = getchar();
  int  nc      = 0;

  while (!AT_END(inpChar)) {
      nc++;
      inpChar = getchar();
  };

  printf("Number of chars=%d\n", nc);
}
Community
  • 1
  • 1
ScottK
  • 1,526
  • 1
  • 16
  • 23