Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Why are these programs not working?

  1. Aug 22, 2009 #1
    I was wondering why are these programs, as given in an e-book not working --

    Code (Text):
     #include <stdio.h>

       /* count lines in input */
           int c, nl;

           nl = 0;
           while ((c = getchar()) != EOF)
               if (c == '\n')
           printf("%d\n", nl);
    Code (Text):

     #include <stdio.h>

       /* count digits, white space, others */
           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')
               else if (c == ' ' || c == '\n' || c == '\t')

           printf("digits =");
           for (i = 0; i < 10; ++i)
               printf(" %d", ndigit[i]);
           printf(", white space = %d, other = %d\n",
               nwhite, nother);
    The programs are getting compiled, but on running the binary, I just get an empty line on which I can type...

    Expected is that after entering a new line, the program should proceed, but nothing like that is happening...after entering a new line...I just enter a new line and can type in that too. :confused:

    This forms a never ending loop.

    I'm using gcc to compile the program.
  2. jcsd
  3. Aug 22, 2009 #2
    Enter the end-of-stream character to terminate the input. Probably ctrl-D.
  4. Aug 22, 2009 #3
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Why are these programs not working?
  1. How do programs work? (Replies: 5)