Basic Help If and Else

I had this on my study guide for the test this week. How would you code this to make it more simpler? I just wanted to see how you would make simpler so that I know for the test. Also how do tag code? I doubt I did it correctly....
#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
int score;
cout << "Enter the test score";
cin >> score;
[f] (score >= 0 && score <= 100)
{

// Improve the following code segment
[if] (score >= 90 && score <= 100)
cout << "A" << endl;
[if] (score >= 80 && score <= 89)
cout << "B" << endl;
[if] (score >= 70 && score <= 79)
cout << "C" << endl;
[if] (score >= 60 && score <= 69)
cout << "D" << endl;
[if] (score >= 0 && score <= 59)
cout << "F" << endl;
}
[else]
cout << "Invalid score" << endl;


return 0;
}

Last edited on
To do code tags you surround your code with [ Code ] [ /Code ] (No spaces)

Apart from you should be using else if on the B/C/D/F scores to make it easier to read and optimize it slightly your code is fine. If you want something different you could do something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 #include <iostream>
#include <string>

int main()
{
    const std::string Grades = "FFFFFFDCBAA";
    
    int score = 0;
    std::cin >> score;
    
    if (score >= 0 && score <= 100)
    {
        std::cout << Grades[score / 10];
    }
}
Topic archived. No new replies allowed.