Thread: Newbie Question

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Newbie Question

    I just baught this book called "The C programming language" and there is an example in the book for counting character input but for some reason it doesn't print out the number on the screen, can someone tell me what i am doing wrong.

    I am using a redhat 9 box.

    Code:
    #include <stdio.h>
    
    main()
    {
    double nc;
    
    for (nc = 0; getchar() != EOF; ++nc)
                        ;
    printf("%.0f\n", nc);
    
    }
    Last edited by Salem; 04-29-2004 at 12:50 AM. Reason: Fixed the code tags

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    I ran a quick test using this code
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int nc;
    	for (nc = 0;getchar()!='\n';nc++)
    		;
    
    	printf ("%d\n", nc);
    
    	return 0;
    }
    It worked just fine and printed an answer. I'm not sure why you were using a double, but an int should work. Also, I replaced the EOF with '\n' so it will stop counting when I hit enter.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for (nc = 0; getchar() != EOF; ++nc)
    ;
    That's your problem. You're basicly making your loop do nothing at all. Well it does something, just not what you expect, since the printf ends up outside the loop due to the semi-colon.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    my guess is you're not ending it with a proper EOF (ctrl+d) instead doing Ctrl+C to break the program, thus it wont print the result.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yeah, just type in a few lines and then press ctrl-d to signal EOF
    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.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Thanx

    Thank you all for helping i really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM