Line Counting Program

This is a discussion on Line Counting Program within the C Programming forums, part of the General Programming Boards category; I'm learning the C Programming Language, and I'm using the book "The C Programming Language, Second Edition" by Kernighan and ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    14

    Thumbs down Line Counting Program

    I'm learning the C Programming Language, and I'm using the book "The C Programming Language, Second Edition" by Kernighan and Ritchie.

    I was using Miracle C for my compiler and everything was working okay, until i got to the Line Counting Program they had there:

    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);
    }
    It makes perfect sense to me and it should work, but instead, when you type something and press enter, it just moves onto the next line for you to type-- the line count variable (nl) is never printed to the output.

    I thought it might be the compiler... I tried it with compiler "Dev-C++" (its both a C++ and C compiler) and got the same problem. Is it a problem with the code? Is this book flawed?

    Thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The printf call is made after the while loop is broken by an EOF. In a Windows console you can generate an EOF with ctrl-z; in *nix I believe it is ctrl-d. Also, you could pipe a file to your executable. For example:
    Code:
    C:\Test>type test.c | test.exe
    13
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    14
    Oh I see. I'm using Window XP, with the "Command Prompt", and Ctrl+d doesn't work for me (it just inputs "^D"). It's pretty hard for me to believe that there's no way to simply input the EOF statement so that the program can continue and print the value.

    In addition, I had no problem with the following code:
    Code:
    #include <stdio.h>
    
    /* copy input to output; 1st version */
    main()
    {
    	int c;
    	
    	c = getchar();
    	while ((c = getchar()) != EOF) {
    		putchar(c);
    	}
    }
    Why is the Line Counting program working and the File Copying program above does work? What can I do to fix the Line Counting program?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I'm using Window XP, with the "Command Prompt", and Ctrl+d doesn't work for me (it just inputs "^D").
    a Windows console you can generate an EOF with ctrl-z;
    Bolding mine

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    14
    Aaah. I tried Ctrl+Z before, but now I tried pressing enter and putting it alone in a line. That did the trick. Thanks guys.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > I was using Miracle C for my compiler
    Nice use of the past tense there.
    The library support is spartan, and it isn't even close to ANSI-C.

    > I tried it with compiler "Dev-C++"
    Now that's more like it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashes on reading a line from a file
    By bluescreen in forum C Programming
    Replies: 5
    Last Post: 10-19-2006, 05:23 AM
  2. help with letter counting part of program
    By bluegoo06 in forum C++ Programming
    Replies: 7
    Last Post: 04-26-2005, 08:25 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. counting program worked, but test file CRASHES IT.. WHY?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2002, 02:29 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21