简体   繁体   English

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

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

Below is a simple program that prompts user to enter number. 下面是一个简单的程序,提示用户输入数字。 The user is assigned a fail, pass, merit or distinction grade based on the value entered: 根据输入的值,为用户分配不及格,及格,优异或优异成绩:

#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;
}

Attempting to compile the above code produces following errors: 尝试编译以上代码会产生以下错误:

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

I've tried adding parentheses around >30 && <=40 and >41 && <=50 , which produced more errors. 我尝试在>30 && <=40>41 && <=50周围添加括号,这会产生更多错误。

Where am I going wrong? 我要去哪里错了?

should be 应该

yourScore>30 && yourScore<=40 yourScore> 30 && yourScore <= 40

imagine that each condition accounts for a true or false value. 想象每个条件都说明一个真值或假值。 in your code (<=40) is not an actual boolean expression. 在您的代码中(<= 40)不是实际的布尔表达式。 you have to have to operands 你必须要操作数

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

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