2012-02-21 129 views
1

下面是一个提示用户输入数字的简单程序。用户被分配一个失败,传球,优点或区别等级根据所输入的值:错误:'<='令牌之前预期的主表达式

#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之间添加括号,这会产生更多错误。

我哪里错了?

回答

4

应该

yourScore>30 && yourScore<=40

想象,每个条件占了true或false值。在您的代码中(< = 40)不是实际的布尔表达式。你必须有操作数

+0

完美,谢谢。 – unpossible 2012-02-21 11:57:26

相关问题