error: 'else' without a previous 'if'

error: 'else' without a previous 'if' even if there is a if statement, please tell me how to fix it :'))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <iostream>
using namespace std;

int main()

{
    int side1, side2, side3;
    
    cout << "Enter all 3 sides: ";
    cin >> side1 >> side2 >> side3;

    if ((side1 == side2 && side2 == side3))
        {
        cout << "This triangle is an equilateral triangle.";
        }

    else ((side1 != side2 && side1 == side3));
        {
        cout << "This triangle is an isosceles triangle.";
        }
    

    if ((side1 != side2 && side2 != side3 && side1 != side3));
        {
        cout << "This triangle is a scalene triangle.";
        }

    return 0;
    
}
Last edited on
the code you posted compiles.
however, the else is all wrong.
-> else has no condition. if you need to chain them, you use the concept of else-if. C++ does not have an explicit else-if keyword, but you can simply say else if (condition) on one line to mimic it as whitespace in c++ is mostly irrelevant.

-> the junk after the else does nothing. its like saying
x; //you can do it, but it does nothing.

1
2
3
4
5
6
7
8
9
10
you want
else if ( side1 != side2 && ... stuff) //no ; here
{
   isoceles
}
else if (side stuff )
{
  scalene
}


to put it in short, you have managed to cobble together 3 got-ya problems in c++:
1) the do-nothing statement after the else
2) the {} block after the else (arbitrary block that looks like it belongs to else, but is just a free-standing block, which is allowed and on rare occasions useful to scope a local variable)
3) the if ; problem. ; means do nothing when it is by itself. see line 24. you have an if and then if its true, do nothing, then always do the block after that, which is disguised as belonging to the if.
Last edited on
Topic archived. No new replies allowed.