C code not working

This is a discussion on C code not working within the C Programming forums, part of the General Programming Boards category; I am dealing with code from The C Programming Language , and can't figure out why it's not working. I ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    1

    C code not working

    I am dealing with code from The C Programming Language, and can't figure out why it's not working. I believe the program is supposed to count how many characters are in a text stream typed, but whenever you type something, it doesn't output anything. I am running Windows XP, using Dev-C++. Here is the code:

    Code:
    #include <stdio.h>
    
    main()
    {
          long nc;
          
          nc = 0;
          while (getchar() != EOF)
                ++nc;
          printf("%ld\n", nc);
    }
    Does anyone have any ideas of how to fix my code, or what is going wrong? I have tried all my ideas.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    It won't output anything until it reads EOF. That means you can either run the program, type some junk and then press CTRL-D (or CTRL-Z in Windows), or you can run it like nc < file.txt.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Code:
    itsme@itsme:~/C$ ./nc
    some junk
    ^D10
    itsme@itsme:~/C$ cat file.txt
    some junk
    itsme@itsme:~/C$ ./nc < file.txt
    10
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with previously working internet code
    By abachler in forum Windows Programming
    Replies: 11
    Last Post: 04-10-2009, 04:11 AM
  2. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  3. Replies: 3
    Last Post: 02-24-2009, 07:49 PM
  4. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 11:00 AM

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