9
Contributors
10
Replies
13
Views
12 Years
Discussion Span
Last Post by CezarPt
0

You have this kind of statement

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

in your code?

0

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

0

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

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

0

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

0

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

0

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 by kirennian: n/a

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.