Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF...ELSE loop question

Status
Not open for further replies.

pghsteelers

Technical User
Apr 21, 2006
121
US
Can anyone answer this very lame question that I just can't see why....why does it return the error

"error C2181: illegal else without matching if"

If I remove the cout line for the first expression it will stop giving me the error. So I don't see what is wrong with the line

if (n == 1)
cout << "N is " << n << endl;
return 1;
else
return n;
 
You need braces to indicate that the if block is more than one statement:
Code:
if (n == 1)
{
    cout << "N is " << n << endl;    
    return 1;
}
else
{
    return n;
}
The braces aren't required on the else block since it only has one statement, but it's highly recommended for readability...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top