newbie counting character and displaying

This is a discussion on newbie counting character and displaying within the C Programming forums, part of the General Programming Boards category; Hello I was just doing an exercise in K&R2 and I want the program to count the characters that I ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    12

    newbie counting character and displaying

    Hello I was just doing an exercise in K&R2 and I want the program to count the characters that I type in and display it. However when I type something in the command prompt, it does nothing.

    Below is what I have. Thank you very much for taking the time to read a newbie's question.

    Code:
    #include <stdio.h>
    
    /* count characters in input; 1st version */
    main()
    {
    	long nc;
    
    
    	nc = 0;
    	while (getchar() != EOF)
    		++nc;
    	printf("%ld\n", nc);
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Hi there,

    Looks good to me, certainly the right idea.

    By "it does nothing" I'm guessing that you mean it doesn't terminate after you've sent EOF?

    That's to be expected. The program will stop on getchar() and wait for you to enter stuff. You could tyoe as much as you want: it won't return a character from getchar() until you press enter, or send EOF. EOF in that case indicates that you're done inputting data and getchar() should do something: but it doesn't actually sentd an EOF char to getchar(). So the loop won't terminate.

    On Linux, you need to do the EOF at the beginning of a line.
    I can't remember exactly how it is on Windows -- I know it definitely won't read anything until you press enter. I'm guessing you're on Windows because you said "command prompt". If you do:
    <some words> + <enter> CTRL-Z
    or
    <some words> + CTRL-Z + CTRL-Z

    I think both of those should terminate the program. You could put a putc() with your counter and see when the letters are counted, if that makes it any cleaer.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    12
    Thank you very much smokeyangel, <some words> + <enter> + CTRL-Z + <enter> did the trick. Kudos to you smokeyangel.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting/displaying number of letters per line?
    By blindchicken11 in forum C Programming
    Replies: 3
    Last Post: 12-06-2011, 04:12 PM
  2. Counting character need help
    By hergelenem in forum C Programming
    Replies: 4
    Last Post: 12-21-2010, 03:18 PM
  3. Help with a character counting program!
    By Redpred in forum C Programming
    Replies: 9
    Last Post: 08-09-2006, 09:15 AM
  4. Character counting
    By Jas11 in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 03:55 AM
  5. Character Counting
    By glider_pilot123 in forum C Programming
    Replies: 6
    Last Post: 11-15-2004, 05:57 PM

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