while ((c = getchar()) != EOF)
if (c == '\n')
++n1;
printf("%d\n", n1);
}
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
It works for me:
% gcc c.c -o c
% ./c < c.c
12
"main()" should be "int main(void)", and you should add a "return
0;" before the closing "}", but neither of those should cause the
problem you're reporting.
Are you sure that the code you posted is *exactly* the same as the
code you're compiling? If you re-typed it into your news client
rather than copy-and-pasting it, you could have introduced (or
corrected!) some seemingly minor error that changes the behavior
of the code.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
How did you run it? Is it still waiting for input? Your program
reads from stdin, so you should type the input, followed by a
character used to signal end-of-file at the beginning of a line.
That character is typically ^Z for Windows and ^D for UNIX.
Or you could redirect input from a file.
I suspect that your problem is that the 'return' key does not generate '\n'
Different system encode a newline differently. Typically it is a
combination of (or just one of) carriage return (\r) and linefeed (\l I
think)
getchar reads single bytes and not a textual decoding of one or more bytes.
> new to programming got some programming logic under my belt. but
> im having some problems trying to get it to work and frustrating
> when its an example in the book -.-' ive complied on both nix
> (gcc version 4.3.3) and windowd (dev++ mingw iirc) and it still
> spits out nothing. #include <stdio.h>
> main() /* count lines in input */
int main(void)
> {
> int c, n1;
> n1 = 0;
>
> while ((c = getchar()) != EOF)
> if (c == '\n')
> ++n1;
>
> printf("%d\n", n1);
return 0;
> }
I see the expected output when running the above code. What does
the data you're using in standard input look like?
--
"Don't worry about efficiency until you've attained correctness."
~ Eric Sosman, comp.lang.c
In that code it should, stdin is a text stream and in text streams the
line break is represented by \n, the encoding used by the OS will be
translated by the standard library if neccessary.
Wrong and wrong. Unix uses plain "\n", Windows uses "\r\n". In both
cases, his program should work.
Older Macs (System 9 and older) use "\r", but nobody has used that for
nearly ten years. Mac OS X uses "\n". He hasn't said anything about
using either of those.
There is no such thing as "\l".
The most likely cause of his problem is that he tried to input data from
the console and did not terminate his input (^D in Unix, ^Z in Windows),
so his program is still waiting for more.
Alternatively, but far less likely, his compiler enforces the C standard
strictly and produced a non-working program due to either or all of the
(at least) three errors that would result in undefined behavior.
DES
--
Dag-Erling Smørgrav - d...@des.no
> How did you run it? Is it still waiting for input? Your program
> reads from stdin, so you should type the input, followed by a
> character used to signal end-of-file at the beginning of a line.
> That character is typically ^Z for Windows and ^D for UNIX.
>
> Or you could redirect input from a file.
I first tried to run it from code::blocks with build n run, i could
input numbers and return put me on a new line but no counting of
lines. I then tried the command line and that was a no go too.
but ^D worked
thanks for all your inputs