views:

59

answers:

6

Hello..

I'm trying to work through a book on C and I am stuck on the following error:

while((c = getchar()) != EOF){
            if(c >= '0' && c <= '9'){
            ++ndigit[c-'0'];
            }
            else if (c == ' ' || c == '\n' || == c =='\t'){
                ++nwhite;
            }
                else{
                    ++nother;
                }
    }

The compiler is complaining about my comparison of var 'c' and the whitespace chars. error: expected primary-expression before '==' token

I haven't written C since school so I am confused as to what is wrong with my syntax. Thanks.

A: 

You wrote:

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

But it should be

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

Notice the == before the last part of the conditional that is removed, in the second snippet. Even if you haven't written C in a while, it looks like that was a simple typo rather than a misunderstanding of those operators.

Mark Rushakoff
You want points for this????? SO is sick; it has a disease.
Heath Hunnicutt
Ha! Even looking at you answer it took me a second to see the extra '=='. I should have taken today off. Thanks.
Nick
+3  A: 
else if (c == ' ' || c == '\n' || == c =='\t'){
                                  ^^
                                   |
                                   +-- This == should be deleted.
Heath Hunnicutt
A: 

Get rid of the == c on the else if line:

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

JonH
A: 

Well, that == in

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

                             ^^ Here

makes no sense whatsoever. Why did you put it in there?

AndreyT
WHOOT usenet highlighting!
Heath Hunnicutt
A: 

this is your problem " == c =='\t')" get rid of the == to the left of the c variable

ennuikiller
A: 

The problem is on the line else if (c == ' ' || c == '\n' || == c =='\t'){

It's that == c == '\t' at the end that's throwing it off. C doesn't allow you to string together comparisons and, additionally, there's nothing to the left of the == (that's what it means by "expected primary expression before ==", it wants something to compare to but there's nothing).

It should be else if (c == ' ' || c == '\n' || c =='\t'){

Alex Zylman