C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2010, 10:41 AM   #1
Registered User
 
Join Date: Feb 2010
Posts: 31
Question No return on char counting program

When I execute the following piece of code I'm prompted to typed in some characters, however nothing happens when I press return. Surely a value should be returned indicating how many characters are contained in the input?

Code:
#include <stdio.h>

main()
{
long nc;
nc = 0;
while(getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
metros is offline   Reply With Quote
Old 02-09-2010, 10:56 AM   #2
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 887
Ugh, formatting...

Code:
#include <stdio.h>

main()
{
   long nc;
   nc = 0;
   while(getchar() != EOF)
      ++nc;
   printf("%ld\n", nc);
}
Thats a little better and isn't the code clearer now too?
As for your question here is a hint: EOF != '\n' (or maybe '\r' depending on platform)
__________________
C/C++ Environment: GNU CC/Emacs
Make system: CMake
Debuggers: Valgrind/GDB
jeffcobb is offline   Reply With Quote
Old 02-09-2010, 10:59 AM   #3
dat is, vast staat
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 6,612
Because pressing return does not indicate EOF.

Try using:
Code:
while(getchar() != '\n')
instead.
__________________
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2
cpwiki -- our wiki on sourceforge
MK27 is offline   Reply With Quote
Old 02-09-2010, 11:02 AM   #4
Registered User
 
Join Date: Feb 2010
Posts: 31
Thanks all. One more thing..... when the program is running, how do I escape back to normal terminal node?
metros is offline   Reply With Quote
Old 02-09-2010, 11:07 AM   #5
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 887
Well, once you escape your while() loop with either CTRL-D (EOF) or ENTER ('\n') it should print the number of characters and exit for you...
__________________
C/C++ Environment: GNU CC/Emacs
Make system: CMake
Debuggers: Valgrind/GDB
jeffcobb is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling Program as .exe with no dependancy on other files nitesh C++ Programming 2 11-01-2009 09:54 AM
How can I make this code more elegant? ejohns85 C++ Programming 3 04-02-2009 08:55 AM
New string functions Elysia C Programming 11 03-28-2009 05:03 AM
OpenGL Window Morgul Game Programming 1 05-15-2005 12:34 PM
fopen(); GanglyLamb C Programming 8 11-03-2002 12:39 PM


All times are GMT -6. The time now is 12:19 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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