guys pls help me , i just a beginner of C++ . This below C++ is writen by me , when i run it , the error : "else" without previous "if" appeared . what point i done wrong !!!!!! and why i run it , the result of "f" is 53 instead of 54
~Here is my code
int main()
{
float a;
float b;
int c ;
float f;
c = 10;
b = 55/12-10;
a = 99/2;
f = a + b + c;
cout << f << endl;
if( f < 0 );
{
cout << " f is negative ";
}
else //(f > 0))
{
cout << " f is positive ";
}
return 0;
}
2) Why are you declaring "extern" variables at global scope? You shadow those with local variables in main, and nothing in your code suggests you'll be linking with any other code that defines those global variables.
3) Look at your "if" statement:
1 2 3 4
if( f < 0 );
{
cout << " f is negative ";
}
The ; at the end acts as an empty statement, finishing the "if" statement. What you've written is the equivalent of:
1 2 3 4 5 6 7 8 9
if( f < 0 )
{
;
} // the entire "if" statement finishes here
// The following block is independent, and is always executed; it has nothing to with your "if" statement.
{
cout << " f is negative ";
}
From this, it should be easy to see why your compiler report the "else" block as not being part of an "if" statement.