0

i was practicing the line counting program. I'm a little bit confused how should i be giving input to this program. I tried typing random sentences including '\n' - the new line element inside my program. But no matter what output i give , the output is still blank and asking me to keep typing. I've went through Internet for a while , and i couldn't figure out where i failed. Can you help me please. I was going to skip this lesson and come back again. But the later lessons of the text book depends on this topic . Thanks

#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);
    }
  • Just redirect the file to your program, e..g `./your_prog < your_file`. That will redirect `your_file` as input to your program on `stdin`. (works in Linux, Windows or Mac) – David C. Rankin Oct 30 '20 at 07:32
  • 2
    When using your keyboard, type some stuff (including newlines if desired) and at the end type `` on Windows or `` on Un*x. – pmg Oct 30 '20 at 07:34
  • @DavidC.Rankin hey i'm kinda confused here. do i need to provide an external text file for this program ? i thought this was asking user input – Abhishek N Oct 30 '20 at 07:34
  • No, you can do it both ways. The key is you have to indicate `end-of-input` (`EOF`) to `getchar()` so your loop exits. Otherwise, `getchar()` just sits there blocking -- waiting on the next input. A redirected file automatically generates an `EOF` when you reach the end. When working from the keyboard -- you have to generate a manual `EOF` as @pmg describes. (you can also use a *heredoc* -- or *herestring* in bash...) – David C. Rankin Oct 30 '20 at 07:37
  • @pmg that worked bro . but it only counts how many times i pressed the 'ENTER' key. it doesnt count how many times did i added '\n' – Abhishek N Oct 30 '20 at 07:38
  • The **Enter** key is what generates the `'\n'`. You entering `'\n'` is interpreted as `'''` followed by a literal `'n'` and another single-quote. – David C. Rankin Oct 30 '20 at 07:39
  • You can pipe input to your program, e..g `printf "my\ndog\nhas\nfleas\n" | ./your_prog` – David C. Rankin Oct 30 '20 at 07:43
  • 1
    @DavidC.Rankin it makes sense now . Thanks David for your precious time and explanation. This kind of support always keeps me motivated to learn new stuff – Abhishek N Oct 30 '20 at 07:46

1 Answers1

1

So, the output is still blank because your program's stuck at a while loop, since the code beneath it is not between curly brackets ({}), that way, your program is asking for input characters indefinitely with no outputs as a result.

Also, I was a little confused by your code since I didn't how to get to EOF through stdin without using file redirection. And by searching through Stack Overflow I've found out that you need to type Ctrl+D, at UNIX-based systems, or Ctrl+Z, on Windows, in order to use EOF through console typing. (End of File(EOF) of Standard input stream (stdin))

By the way, I've modified your code, I just don't know if that was exactly what you were trying to do:

#include <stdio.h>
/* count lines in input */
main()
{
    int c, nl;
    nl = 0;
    while ((c = getchar()) != EOF)//EOF can be reached by stdin when pressing Ctrl+D
    {
        if (c == '\n')
        {
            ++nl;
        }
        
    }
    printf("Number of lines: %d\n", nl);
}

Good coding!