this is a code from the book "the C programming language" by dennis ritchie, but for some reason its not working!

#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */
main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c = '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}

the output shows "nw" (for NewWord) as 0 all the time..
cant understand why this is so...

this is a code from the book "the C programming language" by dennis ritchie

I'm looking at the code in the book right now. Your code doesn't look like the code in the book. They wrote the code with indentation so that it's easy to follow.

Look at the big IF statement. 2/3 of the statement is good, but 1/3 is wrong (it's wrong in the book, too). What do you see that's not consisent? I velieve if you look closer, there is either an error or warning for that line.

Your code doesn't look like the code in the book

i copy pasted from the book and not from my programmer window just to be sure. perhaps copying from a pdf file included some errors. let me check with my hardcopy once more...

What do you see that's not consisent?

the count for line, and character are coming out fine, but the count for new word is always showing 0.

i checked both my pdf and hardcopy with the code i posted, and its the same!!
for_dani_web

this is the output im getting... word count=0 , others (line count=1, character count=23) seems to be doing fine.

I velieve if you look closer, there is either an error or warning for that line.

i read that || is left-to-right operator, and i also tried putting each of the conditions in bracket to eliminate any problems due to matters of precedence and priority... but still i get the same result.

if (c == ' ' || c == '\n' || c = '\t')

One of these things is not like the other. Look very closely, I'm sure the book didn't mix up operators.

@deceptikon : u make it so easy :) thanks.. found it :)
it should be c=='\t'
i also now get what waltP was saying..
sorry to trouble u guys with such silly things. should have paid more attention.

thanks again :)
somjit.

@deceptikon : u make it so easy :)

That's the result of making the same error a bajillion times. It kind of stands out now. ;)

I'm sure the book didn't mix up operators.

Actually, yes it did. The statement is wrong in the book -- at least the PDF I have.

The statement is wrong in the book -- at least the PDF I have.

I'm not aware of an official PDF version, so I'd suspect that it's a bootleg copy and contains copying errors. Unfortunately, I don't have my hardcopy available at the moment, so I can't confirm or deny the typo.

its correct in the hardcopy,checked it, and perhaps, since i copied from the soft version(yeah got too lazy to type there..) that included the mistake, the typo also came in the code, and hence this thread to begin with.. :D

Interesting... Did someone actually type in each page of the book for the .pdf? Rhetorical question...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.