关于if和else语句的预期语句出错

时间:2016-05-24 16:12:59

标签: c++

在我的比较问题中,我使用了if和else语句,因此我收到了错误。

这是我的代码:

#include <iostream>

using namespace std;

int main() {

    double lgt, hgt, lgt1, hgt1, x, y;

    cout << "enter the length of retangle 1= ";
    cin >> lgt;
    cout << "Enter the height of rectangle 1= ";
    cin >> hgt;
    cout << "enter the length of rectangle 2= ";
    cin >> lgt1;
    cout << "enter the height of rectangle 2= ";
    cin >> hgt1;

    x = lgt*hgt;
    y = lgt1*hgt1;

    if (x == y); {
        cout << "both is same" << endl;
    }   
    else if (x > y);{
        cout << "rectangle 1 is bigger" << endl;
    }
    else {
        cout << "rectangle 2 is bigger" << endl;
    }



    return 0;
}

导致此错误发生的原因是什么?

2 个答案:

答案 0 :(得分:1)

您的第一个其他条件中有一个错误的分号(;)。代码应更改为:

else if (x > y) {

答案 1 :(得分:1)

在分支语句中的布尔表达式后面加一个分号在语法上是不正确的。分号表示语句的结尾,因此,语句必须有效才能关闭它。