You have this kind of statement

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

in your code?

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

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;
    }
}

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

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. :)

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

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.

Edited 6 Years Ago by kirennian: n/a

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

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

This article has been dead for over six months. Start a new discussion instead.