Thread: count characters in input

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    11

    count characters in input

    can someone explain to me why I don't get an output running the following code:
    Code:
    #include <stdio.h>/* count characters in input; 2nd version */
    main()
    {
        double nc;
        for (nc = 0; getchar() != EOF; ++nc)   ;
        printf("%.0f\n", nc);
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need to press either ctrl-z (DOS/Windows) or ctrl-d (Unix/Linux) to signal the EOF condition to the input stream.
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to terminate the loop by causing it to read and find that end of file has been reached, e.g., by piping in input from the command line instead of interactively entering it, or by triggering it for interactive input by entering CTRL + D or CTRL + Z (depending on your system) on a new line.

    Also, note that your loop has a stray semi-colon that makes it such that the loop body is empty, whereas you probably want the statement containing the printf call to be the loop body. Fixed and properly formatted, your program might look like this:
    Code:
    #include <stdio.h>
    /* count characters in input; 2nd version */
    
    int main(void)
    {
        double nc;
        for (nc = 0; getchar() != EOF; ++nc)
            printf("%.0f\n", nc);
        return 0;
    }
    The return 0; is optional if compiling with respect to C99 or later as a special case for the main function. Note that I explicitly defined the main function as returning an int and having no parameters.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Why are you using a double instead of an integer type for counting?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Count Characters
    By ojhawkins in forum C Programming
    Replies: 4
    Last Post: 02-26-2012, 05:13 PM
  2. count no of characters
    By manzoor in forum C++ Programming
    Replies: 8
    Last Post: 08-27-2008, 08:22 AM
  3. How to count characters, but not spaces?
    By RedZippo in forum C++ Programming
    Replies: 17
    Last Post: 03-29-2004, 05:09 PM
  4. [STRING][to count][characters]
    By krakz in forum C Programming
    Replies: 2
    Last Post: 02-12-2003, 04:18 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

Tags for this Thread