// 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;
    }
    }

Dani AI

Generated

Quick diagnosis: , the program never actually reads a numeric score (it does cin >> grade), you initialized testScore but never assigned user input to it, standalone comparisons like testScore < 60.0; are just boolean expressions and do nothing, and a switch compares exact values — it doesn't check ranges. is right that an if/else ladder is the simplest for ranges, but if you want to keep a switch you can map the score into buckets first.

A compact, robust switch-based approach (different from the snippets already posted) is to read the score into an int, validate it, then switch on testScore/10. Remember to include case 10 so a score of 100 is handled:

#include <iostream>
using namespace std;

int main() {
    int testScore;
    cout << "Enter your test score (0-100): ";
    if (!(cin >> testScore)) { cout << "Invalid input\n"; return 0; }

    if (testScore < 0 || testScore > 100) { cout << "That score isn't valid.\n"; return 0; }

    char grade;
    switch (testScore / 10) {
        case 10: // 100
        case 9:  grade = 'A'; break;
        case 8:  grade = 'B'; break;
        case 7:  grade = 'C'; break;
        case 6:  grade = 'D'; break;
        default: grade = 'F'; break;
    }

    cout << "Your grade is " << grade << ".\n";
    return 0;
}

A few extra tips: compile with warnings enabled (e.g. -Wall -Wextra) to catch mistyped variables, test boundary values (59, 60, 69, 70, …, 100), and if you need fractional scores use double and convert to an integer bucket before switching. If clarity is more important than cleverness, stick with an if/else ladder as suggested.

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'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.