Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple line count errors

2 views
Skip to first unread message

kittahmasta

unread,
Nov 23, 2009, 2:34:57 PM11/23/09
to
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 c, n1;
n1 = 0;

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.

Keith Thompson

unread,
Nov 24, 2009, 4:57:44 AM11/24/09
to
kittahmasta <afterh...@gmail.com> writes:
> 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 c, n1;
> n1 = 0;
>
> while ((c = getchar()) != EOF)
> if (c == '\n')
> ++n1;
>
> printf("%d\n", n1);
> }

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"

Gordon Burditt

unread,
Nov 24, 2009, 4:59:54 AM11/24/09
to
>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.

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.

Francis Glassborow

unread,
Nov 24, 2009, 5:00:23 AM11/24/09
to
kittahmasta wrote:
> 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 c, n1;
> n1 = 0;
>
> while ((c = getchar()) != EOF)
> if (c == '\n')
> ++n1;
>
> printf("%d\n", n1);
> }


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.

Curtis Dyer

unread,
Nov 24, 2009, 5:01:09 AM11/24/09
to
On 23 Nov 2009, kittahmasta <afterh...@gmail.com> wrote:

> 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

Jasen Betts

unread,
Nov 26, 2009, 3:45:49 PM11/26/09
to
On 2009-11-24, Francis Glassborow <francis.g...@btinternet.com> wrote:
> kittahmasta wrote:
>> 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 c, n1;
>> n1 = 0;
>>
>> while ((c = getchar()) != EOF)
>> if (c == '\n')
>> ++n1;
>>
>> printf("%d\n", n1);
>> }
>
>
> I suspect that your problem is that the 'return' key does not generate '\n'

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.

Dag-Erling Smørgrav

unread,
Nov 26, 2009, 3:46:02 PM11/26/09
to
Francis Glassborow <francis.g...@btinternet.com> writes:
> 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)

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

kittahmasta

unread,
Nov 26, 2009, 3:46:28 PM11/26/09
to
On Nov 24, 3:59 am, gordonb.6u...@burditt.org (Gordon Burditt) wrote:

> 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

0 new messages