guys , pls help me , error : "else" without previous "if"

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

#include <iostream>
using namespace std;

extern float a;
extern float b;
extern int c;
extern float f;

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;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
} 
Last edited on
1) When posting code, please use code tags to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

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.
Last edited on
Dont put semicolon ";" on <if( f < 0)>

You put it with semicolon";"...dont put it with semicolon

<if ( f<0)>

Again , I repet if(f<0)
Last edited on
Topic archived. No new replies allowed.