0

This is from K&R, it's supposed to count digits, white space, etc. I compile fine but when I run I just get a blank screen and all I can do is type inputs. Is this not working because the C language is outdated?

#include <stdio.h>

main()
{
int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;
for (i = 0; i < 10; ++i)
    ndigit[i] = 0;

while ((c = getchar()) != EOF)
    if (c >= '0' && c <= '9')
        ++ndigit[c-'0'];
    else if (c == ' ' || c == '\n' || c == '\t')
        ++nwhite;
    else
        ++nother;

printf("digits =");
for (i = 0; i < 10; ++i)
    printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}
Peter M
  • 7,309
  • 3
  • 50
  • 91
kits
  • 609
  • 6
  • 20
  • 1
    define 'not working' – deW1 Dec 20 '14 at 22:30
  • 7
    "not working" in what sense? Does it crash? Do you actually break the while loop by sending EOF (Ctrl+Z)? Does it give wrong output? Describe! – P.P Dec 20 '14 at 22:30
  • 2
    The program won't tell you anything until you send it an `EOF` char. Do you do that? – Peter M Dec 20 '14 at 22:33
  • 3
    [Possibly related.](http://stackoverflow.com/questions/10589637/having-troubles-with-eof-on-windows-7) – r3mainer Dec 20 '14 at 22:34
  • Not working means I run the program and get a blank DOS screen. If I type something it stays there and let's me type more. – kits Dec 20 '14 at 22:41
  • Note that you can add a text file with test data. Then say something like this to read the file from the program: `type sample.txt | my_program.exe` – Morpfh Dec 21 '14 at 01:23

2 Answers2

2

Well to put it simply. This loop will end once you type ctrl + d. If you want for it to end when you press enter, just modify it a bit: change

while ((c = getchar()) != EOF)

to

while ((c = getchar()) != '\n')
Rasty
  • 301
  • 1
  • 10
2

The program is written to get input until an end-of-file character. Since you're using DOS, enter Ctrl+Z to terminate input (you have to hit "enter" after the Ctrl+Z because keyboard input will be line-buffered).

On edit: Ctrl+Z is the end-of-file (EOF) character in the Windows/DOS world. In UNIX/Linux environments, it is (usually) Ctrl+D.

frasnian
  • 1,973
  • 1
  • 14
  • 24
  • Thanks, that worked. I had no idea I had to type the input and Ctrl+Z after it. – kits Dec 20 '14 at 23:22
  • He´s *not* using DOS. – deviantfan Dec 21 '14 at 00:29
  • 1
    @devantian: OP's comment "Not working means I run the program and get a blank DOS screen" led me to believe he was (or at least a Windows command prompt). – frasnian Dec 21 '14 at 00:45
  • The console server, conhost.exe, doesn't special-case Ctrl+Z (i.e. 0x1A), unlike, for example, Ctrl+C. It waits for the enter key before replying to the `SrvReadConsole` LPC request, just like any other cooked read. Ctrl+Z handling is up to the client process, except `ReadFile` (but not `ReadConsole`) returns 0 bytes read when console input starts with 0x1A. Otherwise Ctrl+Z is handled by the C runtime (if it's used), for which even the POSIX layer (e.g. `_read`) has a text mode that treats Ctrl+Z as `EOF`. – Eryk Sun Dec 21 '14 at 05:45