It is not related to your actual question, but it is a problem none the less.
Double numbers can have decimal places, hence the following code will not work correctly
int calcgrade(double average, char& grade)
{
if (average <= 59)
grade = 'F';
else if (average <= 69 && average >= 60)
grade = 'D';
else if (average <= 79 && average >= 70)
grade = 'C';
else if (average <= 89 && average >= 80)
grade = 'B';
else if (average >= 90 && average <= 100)
grade = 'A';
return 0;
}
If the students score was 89.5 for example, the student would not be graded.
Your code should read:
int calcgrade(double average, char& grade)
{
if (average < 60)
grade = 'F';
else if (average < 70)
grade = 'D';
else if (average < 80)
grade = 'C';
else if (average < 90)
grade = 'B';
else
grade = 'A';
return 0;
}
Note that the above code is still vulnerable to
epsilon errors[
^].
In short, a computer cant store numbers to infinite accuracy, and hence the number 90.0 may be stored as 89.999999999999999999 and hence would be graded as a B, although when printed to 2 or 3 decimal places it is 90.00.
There are macros
FLT_EPSILON
and
DLB_EPSILON
which should be added to each test.