loop doesn't stop in example program?

R

randomtalk

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?
 
C

Captain Winston

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.
 
C

CBFalconer

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/>
 
R

randomtalk

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..
 
R

Richard G. Riley

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!
 
C

CBFalconer

.... 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/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,332
Latest member
LeesaButts

Latest Threads

Top