繁体   English   中英

错误:“ <=”令牌之前的预期主表达式

[英]error: expected primary-expression before '<=' token

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

#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 '<=' token main.cpp|25|error: expected primary-expression before '<=' token

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

我要去哪里错了?

应该

yourScore> 30 && yourScore <= 40

想象每个条件都说明一个真值或假值。 在您的代码中(<= 40)不是实际的布尔表达式。 你必须要操作数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM