Hi, i'm a new to C and am trying to teach myself through a book i have been recommended and bought and now, it has given me 3 or 4 of these "counter" programs and none of them seem to print and i cannot work out why, i do have some programming ability as i know a different language but i cant work this out. This is meant to count the amount of characters, lines and words in the input given and when i input something, nothing happens at all, so i cannot work out if it is the loop failing or the print not working.
Thanks for helping and all feedback is much appreciated
Code:
#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);
}