0
// This program uses a switch-case statement to assign a
//letter grade (A, B, C, D, or F) to a number test score.
#include<iostream>
using namespace std;

int main()
{
    char grade;                int  testScore = 10;

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

    {
    case 10:
    case 5:
        testScore < 60.0;
        cout << "Your grade is f.\n";
        break;
    case 6:
        testScore >= 70.0;
        cout << "Your grade is D.\n";
        break;
    case 7:
        testScore >= 80.0;
        cout << "Your grade is C.\n";
        break;
    case 8:
        testScore >= 90.0;
        cout << "Yor grade is B.\n";
        break;
    case 9:
        testScore <= 100.0;
        cout << "Your grade is A.\n";
        break;
    default:
        cout << "That score isn't valid.\n";

        return 0;
    }
    }

Edited by Reverend Jim: Moved to programming forum

2
Contributors
1
Reply
20
Views
5 Months
Discussion Span
Last Post by AssertNull
0

Lines 10, 11, 12 : You ask the user to enter the test score and you will determine the grade from that, but line 12 takes input for grade, not testScore?

testScore < 60.0;

< is used to compare two values. It is not used all by itself in a line.

Switch statements are generally used when there are a handful of values that a variable can hold. They are normally NOT used when checking whether a value is inside a RANGE of values (ie 60 to 70).

Seems to me you should be using if-else if-else code rather than switch code here.

if(testScore < 60)
{
     grade = 'F';
}
else if(testScore < 70)
{
     grade = 'D';
}
else
{
    grade = 'A';
}

Add a few more else if blocks in there for grades 'C' and 'B'.

Edited by AssertNull

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.