2016-02-22 104 views
0

這是我工作的任務:只返回第一條if語句? (C++)

寫,提示用戶輸入贏得了一個療程的點,並確定等級的程序。該程序應顯示等級和等級的消息字符串。使用'if else'語句來確定打印消息字符串的等級和switch語句。

我編寫了代碼,它執行時沒有任何調試錯誤。問題是,無論我輸入什麼作爲我的PointsEarned,我只得到F的輸出!例如,如果我輸入「90」:

Try Harder Next Time. 
GRADE: F 

什麼問題?我是否把我的switch語句放在錯誤的地方?代碼如下:

#include <iostream> 
using namespace std; 
int main() 
{ 
    int PointsEarned; 
    char Grade; 
    cout << "Please input the points earned in the course: "; 
    cin >> PointsEarned; 
    if (0 <= PointsEarned < 60) { 
     Grade = 'F'; 
    } 
    else if (60 <= PointsEarned < 70) { 
     Grade = 'D'; 
    } 
    else if (70 <= PointsEarned < 80) { 
     Grade = 'C'; 
    } 
    else if (80 <= PointsEarned < 90) { 
     Grade = 'B'; 
    } 
    else if (90 <= PointsEarned) { 
     Grade = 'A'; 
    } 
    else{ 
     cout << "That is not a valid input."; 
    } 
    switch (Grade) 
    { 
    case 'F': 
    case 'D': 
     cout << "Try Harder Next Time." << endl; 
     break; 
    case 'C': 
     cout << "Good." << endl; 
     break; 
    case 'B': 
     cout << "Very good." << endl; 
     break; 
    case 'A': 
     cout << "Excellent." << endl; 
     break; 
    default: 
     cout << "Please choose a valid input to receive your grade." << endl; 
    } 
    cout << "GRADE: " << Grade << endl; 
} 
+1

I''m敢肯定你需要的東西就像'if(0 <= PointsEarned && 60> PointsEarned)'一樣。 –

+0

另請參閱http://stackoverflow.com/questions/8889522/is-4-y-1-a-valid-statement-in-c-how-do-you-evaluate-it-if-so – iksemyonov

回答

4

你不能測試多個值,像你這樣的有:

if (0 <= PointsEarned < 60) { 

這不是檢查,如果PointsEarned060之間。只有那些<ONE將在同一時間,這意味着你有效地測試的東西鐵道部elike

if (0 <= true/false) { 

,而不是進行評估。

你需要的東西更像

if ((0 <= PointsEarned) && (PointsEarned < 60)) { 

代替。

5

比較表格0 <= PointsEarned < 60在Python中工作,但不是C++。您應該使用0 <= PointsEarned && PointsEarned < 60

1

if (0 <= PointsEarned < 60) { 更是打破了C++語言,更改爲2有效的比較 - if (0 <= PointsEarned && PointsEarned < 60) {

+0

什麼都沒有*打破C++語言* – SergeyA

+0

錯誤的是,它編譯和評估很好,但不是人們可能期望的結果。 – iksemyonov

0

我重寫代碼,做一些改進:在該行否則,如果(90 <= PointsEarned)我限制它100.Also其他評議指出你不能測試多個值,所以我重寫,因爲他們說。它現在工作正常!

#include <iostream> 
using namespace std; 
int main() 
{ 
    int PointsEarned; 
    char Grade; 
    cout << "Please input the points earned in the course: "; 
    cin >> PointsEarned; 
    if ((0 <= PointsEarned) && (PointsEarned < 60)) { 
     Grade = 'F'; 
    } 
    else if ((60 <= PointsEarned) && (PointsEarned < 70)){ 
     Grade = 'D'; 
    } 
    else if ((70 <= PointsEarned) && (PointsEarned < 80)) { 
     Grade = 'C'; 
    } 
    else if ((80 <= PointsEarned) && (PointsEarned < 90)){ 
     Grade = 'B'; 
    } 
    else if ((90 <= PointsEarned) && (PointsEarned <= 100)) { 
     Grade = 'A'; 
    } 
    else{ 
     cout << "That is not a valid input."; 
    } 
    switch (Grade) 
    { 
    case 'F': 
    case 'D': 
     cout << "Try Harder Next Time." << endl; 
     break; 
    case 'C': 
     cout << "Good." << endl; 
     break; 
    case 'B': 
     cout << "Very good." << endl; 
     break; 
    case 'A': 
     cout << "Excellent." << endl; 
     break; 
    default: 
     cout << "Please choose a valid input to receive your grade." << endl; 
    } 
    cout << "GRADE: " << Grade << endl; 
} 
0

表達:if (0 <= PointsEarned < 60)意味着if ((0 <= PointsEarned) < 60)
這意味着if ((true/false) < 60)

上述表達式是不一樣的,你可能想表達:
if ((0 <= PointsEarned) && (PointsEarned < 60))