C2shiningC
C2shiningC

Reputation: 13

Parse error in code: expected ' ; ' before ' { ' token -- what is causing this?

The error I'm getting is error: expected ' ; ' before ' { ' token

I tried fixing the code by adding ; after if (thisisanumber==5) as well as after else (thisisanumber!=5). While this solves the first error it creates another error that says error: ' else ' without a previous ' if '. I'd really love to know what error I've made in writing the code, thanks.

Here is my code:

#include <iostream>

using namespace std;

int main()
{
    int thisisanumber;

    cout<<"Whats the Password?: ";
    cin>> thisisanumber;
    cin.ignore();
    if (thisisanumber==5) {
        cout<<"You've discovered the password! Wow, you're a genious you should be proud./n";
        }
    else (thisisanumber!=5) {
        cout<<"You've failed in knowing the password and therefore cannot enter, leave and do not come back. Goodbye!/n";
        }
    cin.get();
}

Upvotes: 1

Views: 5247

Answers (3)

Karl Knechtel
Karl Knechtel

Reputation: 61478

What happens is the compiler looks at the following:

else (thisisanumber!=5) {

and thinks to itself:

"OK, here's else. Is the next token if? No. Ok, so this is an else clause, and the next thing is what to do in the else-case. Is the next token {? No. Ok, so in the else-case, we execute a single statement, instead of a block. Is the next token (? Yes. Ok, so our statement is wrapped in parentheses... [insert here: the rest of the thought process for interpreting an expression that's wrapped in parentheses] Ok, there's the matching ). Whew. Now let's just match up the ; for this statement... wait, what's this? A {! That's not right."

The compiler is reading the code one token at a time, left to right. It does not report an error at the point where, in a logical sense that humans understand, the error actually is. It reports an error at the point where, by reading the code one token at a time, left to right, it is first able to detect that something is wrong.

It would be legal to write else (thisisanumber!=5);. That would mean "if the number is not equal to 5 (because the if test failed), then check if the number is not equal to 5, and do nothing with the result of that comparison". Meaningless, but legal. It would also be legal to write else if (thisisanumber!=5) {...}, which is presumably what you meant. That would mean "if the number is not equal to 5 (because the if test failed), and the number is not equal to 5, then do this stuff inside the {}". But this is redundant: given that something is not equal to 5, it is guaranteed to be not equal to 5, so there is no point in specifying the test twice. So we should just write else {...}.

"else" is really a shorter word for "otherwise", and has that purpose in C++ as well.

Upvotes: 2

Alexander Gessler
Alexander Gessler

Reputation: 46607

You don't need another condition as there are only two cases - just use else { ... } and it will catch all cases in which thisisanumber==5 is false.

The structure of an if statement is:

if (condition) { ... }

else if (another condition) { ... }
// ... more conditions

else { ... all cases in which no previous condition matched end up here  ... } 

... but the else if and else parts are always optional.

Upvotes: 3

Greg Hewgill
Greg Hewgill

Reputation: 992767

You're missing a keyword if:

else if (thisisanumber!=5) {
     ^^

Alternately, since the opposite condition to thisisanumber == 5 is that thisisanumber is not 5, you don't need the condition:

else {

Upvotes: 5

Related Questions