错误:"<="标记之前的预期主表达式

error: expected primary-expression before '<=' token

本文关键字:表达式 lt 错误      更新时间:2023-10-16

下面是一个提示用户输入数字的简单程序。根据输入的值为用户分配一个不及格、及格、优秀或优异等级:

#include <iostream>
using namespace std;
// 0-30 Fail
// 31-40 Pass
// 41-50 Merit
// 51 and over Distinction
int main()
{
    int yourScore;
    cout << "Please enter your score: " << endl;
    cin  >> yourScore;
    if (yourScore <=30)
    {
        cout << "Your grade is fail. Better luck next time." << endl;
    }
    else if (yourScore >30 && <=40)
    {
        cout << "Your grade is pass. Good." << endl;
    }
    else if (yourScore >41 && <=50)
    {
        cout << "Your grade is merit. Well done." << endl;
    }
    else
    {
        cout << "Your grade is distinction. Excellent." << endl;
    }
    return 0;
}

试图编译上述代码会产生以下错误:

main.cpp|21|error: expected primary-expression before '<=' tokenmain.cpp|25|error: expected primary-expression before '<=' token

我尝试在>30 && <=40>41 && <=50周围添加括号,这会产生更多错误。

我哪里错了?

应该是

你的分数>30&amp;你的分数<40

假设每个条件都有一个真值或假值。在您的代码中(<=40)不是一个实际的布尔表达式。您必须操作