C ++ switch statement int

I have been working on a switch for integers for the past hour and a half, I know how to switch from char, but it seems very difficult to me. Many tips will be appreciated. The problem I have is that I can’t accept the ratings of more than 100 that this switch currently performs

    int testScore;                     
    cout <<"Enter your test score and I will tell you \n";
    cout <<"the letter grade you earned ";
    cin >> testScore;

    switch(testScore/10)
{ 
    case 10:
    case 9:
        cout <<"Your grade is A.\n";
    break;
    case 8: 
        cout <<"Your grade is B.\n";
    break;
    case 7: 
        cout <<"Your grade is C.\n";
        break;
    case 6: 
            cout << "Your grade is D.\n";
        break;
    case 5: 
            cout << "Your grade is F.\n";
        break;

    default:
        cout << "That score isn’t valid\n";

    }
+3
source share
3 answers

You are divided by 10.0, which is double, and this will not compile. This should be changed to 10. In addition, you must precede the switch command with an if statement, which checks if it is in the valid range.

#include<iostream>
#include<iomanip>
using namespace std;
int main() 
{

int testScore;
cout <<"Enter your test score and I will tell you \n";
cout <<"the letter grade you earned \n";
cin >> testScore;

if (testScore<=100 && testScore>=0)
    switch(testScore/10)
    {
        case 10:
        case 9:
            cout <<"Your grade is A.\n";
            break;
        case 8:
            cout <<"Your grade is B.\n";
            break;
        case 7:
            cout <<"Your grade is C.\n";
            break;
        case 6:
            cout << "Your grade is D.\n";
            break;
        case 5:
            cout << "Your grade is F.\n";
            break;

        default:
            cout << "That score isn’t valid\n";
    }
else
    cout <<"That score isn't valid\n";

return 0;
}
+3
source

"if", "switch". - ( ):

if (testScore >=0 && testScore <= 100)
{
    char grade;

    if (testScore >= 90)
        grade = 'A';
    else if (testScore >= 80)
        grade = 'B';
    else if (testScore >= 70)
        grade = 'C';
    else if (testScore >= 60)
        grade = 'D';
    else
        grade = 'F';

    cout << "Your grade is " << grade << endl;
}
else
{
    cout << "Score of " << testScore << " is not valid" << endl;
}
+1

,

int testScore;                     
    cout <<"Enter your test score and I will tell you \n";
    cout <<"the letter grade you earned ";
    cin >> testScore;

if (testScore >= 0 && testScore <=100)
{

        switch(testScore/10)
    { 
        case 10:
        case 9:
            cout <<"Your grade is A.\n";
        break;
        case 8: 
            cout <<"Your grade is B.\n";
        break;
        case 7: 
            cout <<"Your grade is C.\n";
            break;
        case 6: 
                cout << "Your grade is D.\n";
            break;
        default: 
                cout << "Your grade is F.\n";

        }

}

else 
    cout <<"That score isn't valid" << endl;
0
source

All Articles