### Program grade without using nested ifs

Hi, I am trying to write a program that assigns a number grade (0-100) to a corresponding letter grade. The challenge is that I cannot use nested ifs , else if etc. I can use one (if/else), if it is possible.
For example: 0-59 = F
60-69 = D
70-79 = C
80-89 = B
90 100 = A
If I get a grade that is < 0 or > 100, the user gets one more chance.

I have written most of the code, and it looks something like this:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````#include int main ( ) { int number_grade = 0; char letter_grade = ' '; // User Prompt std::cout << "Enter your numeric grade: " << "\n"; std::cin >> number_grade; if (number_grade < 0 || number_grade > 100) { std::cout << "Grade is not in range. Please enter again" << std::endl; } //end if std::cin >> number_grade; if (number_grade >= 90 && number_grade <= 100) { letter_grade = 'A'; std::cout << letter_grade << std::endl; } //end if if (number_grade >= 80 && number_grade <= 89) { letter_grade = 'B'; std::cout << letter_grade << std::endl; } //end if if (number_grade >= 70 && number_grade <=79) { letter_grade = 'C'; std::cout << letter_grade << std::endl; } //end if if (number_grade >= 60 && number_grade <=69) { letter_grade = 'D'; std::cout << letter_grade << std::endl; } // end if if (number_grade >= 0 && number_grade <= 59) { letter_grade = 'F'; std::cout << letter_grade << std::endl; } //end if std::cin.get(); return (0); } ``````

The program works fine if I enter a number < 0 or > 100 first. However, if I enter a number >= 0 or <= 100 first, it makes me enter the number twice to get the desired grade.

For example:

95
95
A

Can someone point me in the right direction here? And also is there a more efficient way to do this in spite of using the restrictions I specified?

Line 20:
`std::cin >> number_grade;`

You ask for input again. (:
Hint: Integer division.
 The challenge is that I cannot use nested ifs , else if etc.

Can you use a switch statement?
 ``12345678910111213`` ``````switch (number_grade/10) // integer division will give us a number from 0 - 10 { case 9: case 10: letter = 'A'; break; case 8: letter = 'B'; break; case 7: letter = 'C'; break; case 6: letter = 'D'; break; default: letter = 'F'; }``````

 ``12`` ``````const char* GRADES = "FFFFFFDCBAA"; letter = GRADES[ number_grade/10 ];``````
Very nice.
Lookup tables ftw.
Thanks for the replies all. Unfortunately, my professor will not allow switch or nested ifs. It would seem the program would be much easier to write if I was allowed to use them.

The reason I asked for another input in line 20 std:: >> number_grade is because I have to give the user another chance if the grade is not in range. I can take it out but if I do the user will just get a prompt to enter again but will not be able to actually do it unless they start the program over.
 Unfortunately, my professor will not allow switch or nested ifs. It would seem the program would be much easier to write if I was allowed to use them.

Not really. It doesn't get much easier than a lookup table.

See tntxtnt's solution.
Topic archived. No new replies allowed.