Creating simple if statements that use a variety of numbers

+2 Lé Pig · March 2, 2015
Hi everyone. So I'm trying to tackle a very small project that displays grades for scores and I can go the route of creating if statements or switches but I get stumped. Now if I were to use a  simple if (score ==100) or something it lets me display the grade, but if I wanted to display a grade of 90 through 100 how could I write that? I can think of telling the if statement that if 'score' is >89 to print out it's an a, but the next set of numbers 80-89 needs to print out a "B" but if  I use an if statement that tells the computer if  'score' >79 print a "B" now I have an A and a B printing out.  Anyway, thanks for the help.

Post a Reply


Oldest  Newest  Rating
0 Laura Lee · March 2, 2015
You need to use if and else if.
int x = 90;
if(x > 89)
else if(x > 79)

We know that X is greater than both 89 and 79. However, only "A" will be printed because the rest of the conditions use else if.
Only the first valid condition is checked. The rest aren't. B will not be printed.
0 Lé Pig · March 3, 2015
Awesome. Thank you! Straight to the point. Why did you x = to 90 initially?  I'm curious. Thanks again!
0 Earl Hudson · March 4, 2015
It's always best to initialize your variable with a "base" number, or a starting point if you will...

If you just use int x; you have no idea what the value of x is. It is equal to whatever is the the memory location where x resides.

try this

int x; cout << x << endl;
int y; cout << y << endl;
int z; cout << z << endl;

Now try:

int x=10 ; cout << x << endl;
int y=20 ; cout << y << endl;
int z=30 ; cout << z << endl;
+1 Jake Scaife · March 7, 2015
I know this has already been answered, however I figured I would add my 2 cents ;)

When I made this when starting out, instead of using if statements etc, I used a very simple switch statement.

Something like this:

int score;     //This is your percentage score out of 100
String grade;    //This is the grade you want to return

switch(score / 10)   // Divide score by 10 to cover ranges.
    case 6 :
        grade = "D";
    case 7 :
        grade = "C";
    case 8 :
        grade = "B";
    case 9 :
        grade = "A";
    case 10 :
        grade = "A+";
    default :
        grade = "F";

When you divide your score by 10, the switch statement will use the integer value. So 80-89 would fall under the 8 case. etc.
Not saying this method is better than if statements, just something to provoke your thinking! :angel:
0 Earl Hudson · March 7, 2015
Right right... You can send it off to a function as well. C++'s flexibility is a programmers greatest tool, and can be his worst nightmare.

#include <iostream>
#include <string>

using namespace std;

string grader( int grade)
    if ( grade< 61)  {return "a F"; };
    if ((grade >60) && (grade <71)) {return "a D"; };
    if ((grade >70) && (grade <81)) {return "a C"; };
    if ((grade >80) && (grade <91)) {return "a B"; };
    if ((grade >90) && (grade <101)){return "an A"; };
    return "Error - number was out of range.";

int main(){
    int score=1;
    string result="";
    cout << "\t\t\tWelcome to Grader\n\nPress 0 to end.\n\n"<< endl;

    while (score>0){
        cout << "What was the test score? ";
        cin >> score;
        if (score==0){return 0; };
        result = grader(score);
        cout << "You scored " << result << endl;
    return 0;

0 Lé Pig · March 25, 2015
Really useful information guys I really appreciate it
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator