Why are these programs not working?

  • Thread starter Thread starter dE_logics
  • Start date Start date
  • Tags Tags
    Programs
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
dE_logics
Messages
742
Reaction score
0
I was wondering why are these programs, as given in an e-book not working --

Code:
 #include <stdio.h>

   /* count lines in input */
   main()
   {
       int c, nl;

       nl = 0;
       while ((c = getchar()) != EOF)
           if (c == '\n')
               ++nl;
       printf("%d\n", nl);
   }
Code:
 #include <stdio.h>

   /* count digits, white space, others */
   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);
   }

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.
 
Physics news on Phys.org
Enter the end-of-stream character to terminate the input. Probably ctrl-D.