错误:在'< ='标记之前预期的primary-expression

时间:2012-02-21 11:32:10

标签: 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周围添加括号,这会产生更多错误。

我哪里错了?

1 个答案:

答案 0 :(得分:4)

应该是

  

yourScore&gt; 30&amp;&amp; yourScore&LT; = 40

假设每个条件都考虑了真值或假值。在您的代码中(&lt; = 40)不是实际的布尔表达式。你必须要操作数