Can anyone expand on why you get this error?

Re: illegal else without matching if? 80 80

You have this kind of statement

if ()
..........
else
..........
else
........

in your code?

Re: illegal else without matching if? 80 80

You have this kind of statement

if ()
..........
else
..........
else
........

in your code?

{
toss = 1 + rand() % 2;
if
(toss == 1 )
cout << "Heads" << endl;
passes = passes +2;

else
cout << "Tails" << endl;
failure =failure + 1;

}

basically if theres a 1 at int toss then it prints out Heads but i now wish it to add 2 on to passes for every head in printed out, and for every tails i wish for it to +1 to failures.

But i get an illegal error

Re: illegal else without matching if? 80 80

if you have more than one statement in the if/else block you need braces ( { } )

{ 
    toss = 1 + rand() % 2; 
    if(toss == 1 )
    { 
        cout << "Heads" << endl;
        passes = passes +2;
    }
    else 
    {
        cout << "Tails" << endl; 
        failure =failure + 1;
    }
}
Re: illegal else without matching if? 80 80

this can happen only when u had used if statement along with two else which leads to an error......however due to braces u will not get any syntax error but only the logical error is the possiblity

Re: illegal else without matching if? 80 80

Can anyone expand on why you get this error?

You probably have an else statement in your code but no if. It would be nice to see it but what I said above is almost definitly the problem. :)

Re: illegal else without matching if? 80 80

oops, it looks like 1oo0oobp already told you :O

Re: illegal else without matching if? 80 80

if i write a program abt menu of restaurant using if else ...exp:if{a==1)
printf(),else
if(a==2)
print()
else...
it give me illegal if else .so how to solve it

Re: illegal else without matching if? 80 80

You really should sort out a line spacing convention, that just looks horrible.

if{a==1)
  printf();
else if(a==2)
  print();
else
  //do something here otherwise it'll fail.
Re: illegal else without matching if? 80 80

If you carefully read about If...else statement then you must know that else is always used for a matching if that means

if(condition)
{block-of-statement;
}
else
{block-of-statement;
}

rules says that this a structure you have to use for if else statement, if you use more then one ELSE for one IF you get this error message.
Check you program your program must have some thing like this

if(condition)
{block-of-statement;
}
else
{block-of-statement;
}
else
{block-of-statement;
}

Best of luck

Re: illegal else without matching if? 80 80

if (condition) //i left a semicolon after the (statement)that's why i got themessage
{
block-of-statement
}
else
{
block-of-statement;
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.