预期在 if 和 else 语句上的语句上出错

Error on expected a statement on if and else statement

本文关键字:语句 出错 else if      更新时间:2023-10-16

在我的比较问题中,我使用了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;
}

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

你在第一个 else 条件下有一个错误的分号 ( ; )。 代码应更改为:

else if (x > y) {

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