Why are these programs not working?

  • Thread starter Thread starter dE_logics
  • Start date Start date
  • Tags Tags
    Programs
AI Thread Summary
The discussion revolves around issues with two C programs designed to count lines, digits, whitespace, and other characters from user input. Users report that, upon running the compiled binaries, they encounter an empty line where they can type, but the programs do not proceed as expected after entering a new line. This behavior suggests a potential misunderstanding of how to terminate input, as the programs rely on the end-of-file (EOF) signal to stop reading input. The correct method to signal EOF in this context is typically Ctrl-D on Unix-like systems. The programs compile successfully with gcc, but users must ensure they provide the EOF character to exit the input loop and see the expected output.
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.
 
Technology news on Phys.org
Enter the end-of-stream character to terminate the input. Probably ctrl-D.
 
Thanks.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top