simple count line program

This is a discussion on simple count line program within the C Programming forums, part of the General Programming Boards category; hello everyone, i executed this code but the loop never ends because i have no idea what EOF is hence ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43

    simple count line program

    hello everyone,
    i executed this code but the loop never ends because i have no idea what EOF is hence the number of lines that i enter is not printed.
    What do i press to signal EOF when i'm finish entering a suitable number of lines to see if they were counted by the program?(-1,0,1) do not work
    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);
    }
    not my code btw
    Last edited by Shinzu911; 05-02-2012 at 11:56 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    You can trigger EOF by a CTRL + D or CTRL + Z, as the case may be.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking command line argument count [ac],linux
    By bos1234 in forum C Programming
    Replies: 13
    Last Post: 04-12-2012, 06:15 AM
  2. Replies: 3
    Last Post: 02-21-2011, 09:21 PM
  3. Program to count 1 through 9
    By starwarsyeah in forum C Programming
    Replies: 9
    Last Post: 03-09-2009, 01:35 PM
  4. simple program to count the character not working
    By hitesh1511 in forum C Programming
    Replies: 12
    Last Post: 08-08-2006, 04:13 AM
  5. make line count
    By Ron in forum C Programming
    Replies: 7
    Last Post: 07-09-2006, 09:55 AM

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