0

It's confusing because I just started C++ 95 is A+ 90 is A, 85 is B+ 80 is B, 75 is C+ 70 is C... I'm writing a code that separates grades, but it's so hard Where should I fix it?

#include<iostream>
using namespace std;

int main()
{

    int score;
    string grade;

    cout << "Enter a score between 0 and 100 : ";
    cin >> score;

    int scorecal = score / 10;
    scorecal += 5;

    switch (scorecal)
    {
    case 14:
        grade = "A+";
        cout << "Your score is " << grade << endl;
    case 13:
        grade = "A";
        cout << "Your score is " << grade << endl;
        break;
    case 12:
        grade = "B+";
        cout << "Your score is " << grade << endl;
    case 11:
        grade = "B";
        cout << "Your score is " << grade << endl;
        break;
    case 10:
        grade = "C+";
        cout << "Your score is " << grade << endl;
    case 9:
        grade = "C";
        cout << "Your score is " << grade << endl;
        break;
    case 8:
        grade = "D+";
        cout << "Your score is " << grade << endl;
    case 7:
        grade = "D";
        cout << "Your score is " << grade << endl;
        break;
    default:
        grade = "F";
        cout << "Your score is " << grade << endl;
        break;
    }
    
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 5
    You are missing a couple of breaks in the switch statement – Ed Heal Mar 23 '23 at 12:32
  • What is wrong with the code you have? – Yksisarvinen Mar 23 '23 at 12:33
  • 4
    You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Mar 23 '23 at 12:33
  • 1
    BTW if `score` is between 0 and 100, then you divide by 10 - maximum value is 10, not 14 – Ed Heal Mar 23 '23 at 12:34
  • You overlooked the addition of 5, @EdHeal – Sam Varshavchik Mar 23 '23 at 12:35
  • @SamVarshavchik - Oops. You are right. So the max is 15 – Ed Heal Mar 23 '23 at 12:36
  • Why bother dividing the score at all? Change the switch to an if/else block and use the number as-is. – sweenish Mar 23 '23 at 12:37
  • 95 is A+, so 100 is ... undefined! I expect 96 should be A+ too, but the spec doesn't say that, so we have to assume scores are only marked in multiples of 5, and 100% is not a valid result. – gbjbaanb Mar 23 '23 at 12:39
  • OT: you should remove all lines `cout << "Your score is " << grade << endl;` and put one single line `cout << "Your score is " << grade << endl;` right after the `}` of the `switch`. I'd give you at most a D for this code even if it worked fine. BTW is there no E grade? – Jabberwocky Mar 23 '23 at 12:49
  • @Jabberwocky There has never been an 'E' letter grade that I'm aware of, at least with US letter grades. – sweenish Mar 23 '23 at 14:05
  • @sweenish Thanks for the information, I wasn't aware there was a "hole" in the grades. Looks like another US weirdness (like imperial units). – Jabberwocky Mar 23 '23 at 14:10

1 Answers1

2

Why use a switch? You're just making it more complicated than it needs to be. A set of if statements works just as well and is a lot easier to understand. Writing easy-to-understand code is way more important than any micro-=optimisation you might be trying to put in.

eg:

if (score >= 95)
    grade = "A+";
else if (score >= 90)
    grade = "A";
// and so on
else
    grade = "F";

cout << "Your score is " << grade << endl;
gbjbaanb
  • 51,617
  • 12
  • 104
  • 148