What's wrong with this program??

This program does not print what I want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>

int main(int argc, const char * argv[]) {
    int blank = 0;
    int tabs = 0;
    int newlines = 0;
    int c;
    
    printf("Enter some text containing blanks, tabs and newlines: ");
    
    while ((c = getchar()) != EOF) {
        if (c == ' ')
            ++blank;
        else if (c == '\t')
            ++tabs;
        else if (c == '\n')
            ++newlines;
        else
            c = getchar();
    }
    
    printf("There are %d blanks in the text.\n", blank);
    printf("There are %d tabs in the text.\n", tabs);
    printf("There are %d newlines in the text.\n", newlines);
    
    return 0;
}
Last edited on
You don't say what you want. At any rate, I suggest taking a look at lines 11, 18, and 19, then think about what happens there.
It does not count what I need it to count.
As I already said... take a look at those lines and fix them.

while ((c = getchar()) != EOF)

So the loop is supposed to run until it reaches end of file.... Except this is stdin, console input. You'll never reach an end of file. That means this is an infinite loop.

1
2
        else
            c = getchar();

Why do you have this here at all? You're going to skip the next character if the current character isn't one of the things you're keeping track of? Does that make sense?
Topic archived. No new replies allowed.