Hmm some useful code there - as well as some which doesn't look like it can work.
First the straightforward stuff. Somewhere along the line, your letter grade became a type
double
? It should be a
char
, as already stated by Thomas1965.
This test
|
if ((grade < 0) || (grade >= 101))
|
rather strangely tests for values
>= 101
. It should be just
> 100
, shouldn't it? (assuming percentages in usual range of 0 to 100).
All the
const int A
to
const int F
serves to document your intentions. I treat it as comments because it isn't needed in that form as actual code.
I re-arranged the code a little, first get the input, validate that it is in the range 0 to 100 inclusive, Only then, with a clean input value, go on to try to determine the letter for that number. Strictly speaking, all of the foregoing is adding extra features which were not mentioned in the original question in any case.
So now we get to the heart of the problem. I made a start at lines 24 to 35 below. That is the focus of the original problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
using namespace std;
int main()
{
double grade;
cout << "Enter your grade: " << flush;
while (!(cin >> grade))
{
cin.clear();
cin.ignore(10000,'\n');
cout << "***Invalid, please enter a numeric value: ";
}
if ((grade < 0) || (grade > 100))
{
cout << "\n0 - 100 only\n";
return 0;
}
char letterGrade = 'X';
if (grade >= 90)
letterGrade = 'A';
else if (grade >= 80)
letterGrade = 'B';
// ...
// ...
else
letterGrade = 'F';
cout << "your grade of: " << grade
<< "\nIs a letter grade of: " << letterGrade;
}
|