c++ - 错误 : expected primary-expression before '<=' token

标签 c++ compiler-errors

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

#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) 不是实际的 bool 表达式。你必须要操作数

关于c++ - 错误 : expected primary-expression before '<=' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9376823/

相关文章:

c++ - 获取模板可调用对象的参数类型

c++ - Clang++ 编译器 - python 的模拟

c++ - goroutines 和 boost.fiber 之间的区别

c++ - Qt 不支持 VTK 库

c# - 更改 AssemblyVersion 停止项目编译

java - NetBeans 运行 Java 程序,但出现编译时错误

java - 非静态变量不能从静态上下文中引用 - 为什么在这里?

c++ - 使用索引技巧初始化多维数组

java - 调试简单的2层井字游戏编码中的错误?

compiler-errors - 在编译时进行全类型检查