Originally Posted by
quzah
It probably doesn't actually do either. Most input is line buffered, meaning it doesn't actually end up sending \b to your program. Write \b to a file, then read it back in, and run it through the above loop, and you'll more than likely get the correct input. (Or write it out, and pipe it in.)
Quzah.
Hmm... I was afraid of that. I guess I'll have to learn how to input from a file before testing that one.
I really hate to keep bothering you guys, but what about this one?
Code:
#include <stdio.h>
#define IN 1
#define OUT 0
int main () {
/* counts lines, words, and characters in input */
int c, nl, nw, nc, state;
nl = nw = nc = 0;
state = OUT;
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", nc, nl, nw);
}
(Counts characters, words, and lines then outputs this to the screen.) For some reason this program always tells me I'm getting 30 or so more words than I actually input. As far as I can tell, this is the exact code from the book. What's going on here?