0

I'm following along with a book on C to learn it and I am writing all the code in the book to follow along, the latest one to do with arrays is supposed to say how many white spaces, tabs etc. There are but when I execute it, nothing shows up, it's just blank, as in I can type something then press enter and nothing happens, is it supposed to tell me how many of each thing there is?

I am too new to understand if this program is actually supposed to output anything so I thought I would post it on here and get an opinion, it compiles and runs fine, no errors, but the book sort of refers to it outputting stuff, but nothing happens when I'm running it and typing stuff in, can just keep typing stuff forever.

Here is the code

 #include <stdio.h>

 int 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[1]);
    printf(", white space = %d, other = %d\n", nwhite, nother);
 }
Santhosh Pai
  • 2,535
  • 8
  • 28
  • 49

3 Answers3

1

The application takes in input some values and then counts the number of digits (0-9) and the white spaces. The key combination to interrupt the cycle is not ENTER but EOF which in Linux is CRTL-D and in WINDOWS is CTRL-Z.

Then, in you application there is a bug :

  for (i = 0; i < 10; ++i)
        printf(" %d", ndigit[1]);

In order to show the number of digits this should be :

for (i = 0; i < 10; ++i)
    printf(" %d", ndigit[i]);

Unfortunately, getting interactive input is quite problematic when using scanf(), getchar(), fgets(), etc. That's why most people usually write their own custom functions, often getting a whole line from stdin and then parsing it according to their needs. However, if you want to use ENTER to stop the cycle you can modify the code as follows, but you will lose the possibility to count the number of new lines in the input.

#include <stdio.h>

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

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

  while ((c = getchar()) != '\n')
    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); 

return 0; 

}

This should work as you expected. However, you should consider to write a better input function, there are several interesting solution online.

EDIT

The main() must return int. Not void, not bool, not float. int. Just int, nothing but int, only int. Some compilers accept void main(), but that is non-standard and shouldn't be used.

Check some examples here : http://www.parashift.com/c++-faq-lite/main-returns-int.html

Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
  • Thanks guys, didn't know about the CTRL + D thing, thanks for pointing out the bug too, didn't notice that before! And no the book I'm reading is an old one and doesn't say to add void to the main function, so maybe I should change and read a more modern book? Thanks all!! – spergalerger Jul 24 '13 at 09:47
  • The best book to learn C is http://en.wikipedia.org/wiki/The_C_Programming_Language – Giuseppe Pes Jul 24 '13 at 09:52
  • Check out this post: http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – Giuseppe Pes Jul 24 '13 at 09:53
  • I have the one by Brian Kernighan, but it hasn't mentioned anything to do with putting "void" in the main function yet. – spergalerger Jul 24 '13 at 10:17
  • Sorry, you can put void in the main. But it is not a good practice to put void because the main should always return a value. If the program has been correctly executed the correct return value is ZERO or otherwise an error. I modified the code to add this change. – Giuseppe Pes Jul 24 '13 at 10:32
0

You can provide a EOF using following

WINDOWS:

Press F6 then ENTER
or 

Ctrl+Z

LINUX:

Ctrl+D
glglgl
  • 89,107
  • 13
  • 149
  • 217
P0W
  • 46,614
  • 9
  • 72
  • 119
  • Also fix your code its `for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]); //instead of ndigit[1]` – P0W Jul 24 '13 at 09:08
0

Change

printf(" %d", ndigit[1]);

to

printf(" %d", ndigit[i]);

Press ctrl+d to give EOF after entering value

Input:

2 4 6 8     //3 white space + 1 new line = 4 white space
[ctrl + d]

Output:

digits = 0 0 1 0 1 0 1 0 1 0, white space = 4, other =0 
Amarnath Krishnan
  • 1,253
  • 1
  • 9
  • 12