473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

loop doesn't stop in example program?

hello, recently i am reading "The C Programming Language, Second
Edition", and for some reason, the sample program in there doesn't work
as expected under GCC.. here is the code:

#include <stdio.h>

/* count lines in input */
main()
{
int c, nl;

nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}

which should get all the input, count the lines, and return some
number.. However, when i ran it, it just looped input over and over, it
doesn't ever see the EOF for some reason.. So i type in something, hit
enter, it just gives me the input box again, without counting
anything.. anyone know what's wrong?

Mar 22 '06 #1
7 2870

ra********@gmail.com wrote:
hello, recently i am reading "The C Programming Language, Second
Edition", and for some reason, the sample program in there doesn't work
as expected under GCC.. here is the code:

#include <stdio.h>

/* count lines in input */
main()
{
int c, nl;

nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}

which should get all the input, count the lines, and return some
number.. However, when i ran it, it just looped input over and over, it
doesn't ever see the EOF for some reason.. So i type in something, hit
enter, it just gives me the input box again, without counting
anything.. anyone know what's wrong?


If you compiler is under Windows, press Ctrl+Z to issue EOF;
and press Ctrl+D under Unix/Linux.

Mar 22 '06 #2
Thank you very much for replying! It now works as intended.

Mar 22 '06 #3
ra********@gmail.com wrote:

hello, recently i am reading "The C Programming Language, Second
Edition", and for some reason, the sample program in there doesn't
work as expected under GCC.. here is the code:

#include <stdio.h>

/* count lines in input */
main()
{
int c, nl;

nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}

which should get all the input, count the lines, and return some
number.. However, when i ran it, it just looped input over and over, it
doesn't ever see the EOF for some reason.. So i type in something, hit
enter, it just gives me the input box again, without counting
anything.. anyone know what's wrong?


What EOF? How did you signal EOF? Did you redirect input to come
from a file, which has an end (i.e. .\a <somefile>). If inputting
from the keyboard did you signal EOF after entering the lines?

Hint: EOF is normally signalled from the keyboard with CTRL-D on
*ix, and with CTRL-Z on windoze/dos.

Read my sig. and the referenced URLs before replying over the
google system.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Mar 22 '06 #4
> What EOF? How did you signal EOF? Did you redirect input to come
from a file, which has an end (i.e. .\a <somefile>). If inputting
from the keyboard did you signal EOF after entering the lines?

Hint: EOF is normally signalled from the keyboard with CTRL-D on
*ix, and with CTRL-Z on windoze/dos.
well, i originally thought enter would signal EOF.. After several
responses, i now realized that enter only signal end of the line, now
EOF..
Read my sig. and the referenced URLs before replying over the
google system.


thank you very much for pointing that out! I'll use reply from show
options from now on..

Mar 22 '06 #5
ra********@gmail.com wrote:
.... snip ...
thank you very much for pointing that out! I'll use reply from
show options from now on..


Good for you. Having learned that, remember not to strip
attributions for material you quote. Those are the initial lines
of the form "whozit wrote:", and the count of '>' serves to tie
lines to originators.

It looks as if you are a quick study, unlike too many others.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Mar 22 '06 #6
On 2006-03-22, ra********@gmail.com <ra********@gmail.com> wrote:
hello, recently i am reading "The C Programming Language, Second
Edition", and for some reason, the sample program in there doesn't work
as expected under GCC.. here is the code:

#include <stdio.h>

/* count lines in input */
main()
{
int c, nl;

nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}

which should get all the input, count the lines, and return some
number.. However, when i ran it, it just looped input over and over, it
doesn't ever see the EOF for some reason.. So i type in something, hit
enter, it just gives me the input box again, without counting
anything.. anyone know what's wrong?


Hi : Dont confuse EOF with "End Of Line" : EOF is a macro for End of
File. It is described in your book at the start of the "Input Output"
chapter. To generate it from the keyboard you can use "ctrl-d" in
unix/linux : not sure in windows. You have a great book there : It's
rarely wrong :-;

Good luck!
Mar 22 '06 #7
On Wed, 22 Mar 2006 16:14:22 +0100, "Richard G. Riley"
<rg****@gmail.com> wrote:
On 2006-03-22, ra********@gmail.com <ra********@gmail.com> wrote:
hello, recently i am reading "The C Programming Language, Second
Edition", <snip>

<snip> You have a great book there : It's
rarely wrong :-;

And for the few times it is, the errata are online under
http://cm.bell-labs.com/cm/cs/cbook/index.html

- David.Thompson1 at worldnet.att.net
Apr 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
23
by: ern | last post by:
I have a program that runs scripts. If the user types "script myScript.dat" the program will grab commands from the text file, verify correctness, and begin executing the script UNTIL... I need...
12
by: melanieab | last post by:
Hi, I'm trying to check and see if something other than numbers (either the +, -, *, or /) are entered into a textbox, where bigR is what I call the text in the textbox. I can get what was...
8
by: Dave | last post by:
I am trying to write an event-driven application with no main window that runs "forever". It waits on a named event and then displays a window depending on data pased in a memory-mapped file. I...
19
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
70
by: hstagni | last post by:
When i read a key using getchar() inside a loop, the program stops and wait for a key to be pressed. I actually want the program to continue its execution until a key is pressed. Look at this...
19
by: Richard | last post by:
Hi All, I copied a script example from http://www.irt.org/script/640.htm into a local .html file. I opened that file first in HTML-kit, which hung (in an infinite loop, I think) when I...
5
by: david | last post by:
I'm developing a program that runs using an asyncore loop. Right now I can adequately terminate it using Control-C, but as things get refined I need a better way to stop it. I've developed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.