2012-02-21 127 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

相關問題