Thread: counting

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

    counting

    Code:
    #include <stdio.h>
    
    
    main()
    {
        long nc;
    
    
        nc = 0;
        while (getchar() != EOF)
            ++nc;
        printf("%1d\n", nc);
        
    }
    I was just wondering if anyone can explain to me why this piece of simple code isn't working.
    Im using GNU/GCC compiler(although i don't think it has anything to do with it...).
    Thanks,Filster7.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. Are you counting the '\n'?
    2. What are you using to emulate EOF?
    3. Have you tried with simple file passed to stdio of the program?
    4. Do you mean to have %ld instead of %1d format?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    35
    The program was designed to count characters...not the \n.
    I did try the %ld format,but it didn't work....

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    If you're on Linux, you need to press ctrl-d to signal EOF to the program.
    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.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    35

    counting

    Quote Originally Posted by Salem View Post
    If you're on Linux, you need to press ctrl-d to signal EOF to the program.
    Im using Windows OS-64bit.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    In that case, try ctrl-z after you have pressed enter.
    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.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    35
    How embarassing,i already did that,but didn't notice anything.Thanks for solving this.Sorry to bother you...

  8. #8
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    89
    Quote Originally Posted by Filster7 View Post
    Im using Windows OS-64bit.
    What isn't working?
    The following works for me:
    Code:
    #include <stdio.h>
     
    main()
    {
       long nc;
     
     
       nc = 0;
       while (getchar() != EOF)
          ++nc;
       printf("%ld\n", nc);
         
    }
    Testing it with the source code itself as input:
    Code:
    Test$ ls -lB | grep getcharTest.c 
    -rw-rw-r-- 1 guraknugen guraknugen  137 jul 13 20:17 getcharTest.c
    Test$ ./a.out < getcharTest.c 
    137
    Test$

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting
    By r_1481 in forum C Programming
    Replies: 4
    Last Post: 11-25-2011, 02:55 PM
  2. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  3. counting down...
    By twomers in forum C++ Programming
    Replies: 5
    Last Post: 12-09-2005, 02:51 PM
  4. counting
    By threahdead in forum C Programming
    Replies: 0
    Last Post: 10-19-2002, 10:33 AM
  5. Counting
    By simhap in forum C++ Programming
    Replies: 5
    Last Post: 10-08-2001, 04:06 PM