I have this if else statement in a program to calculate three grades from multiple students. I can't figure out why this function will always assign 'P' to letter.
The function is reading in the grades from the main program, which is reading in from a file. The grades are all out of 100 (I've been using 58 99 45 to test)
//function to calculate and return average with letter grade
void judgement(double grade1, double grade2, double grade3, double &average, char &letter)
{
average = (grade1 + grade2 + grade3)/3;
if (average <= 70)
letter = 'P';
else
letter = 'F';
return;
}
If somebody could give me an explanation, that would be much appreciated.
http://ideone.com/Wp43GQ
If it is less than or equal to a passing grade, it sets letter to P. If it is greater than a passing grade, it sets letter to F. This is what you wrote, but it most obviously is not what you meant.
//function to calculate and return average with letter grade
void judgement(double grade1, double grade2, double grade3, double &average, char &letter)
{
average = (grade1 + grade2 + grade3)/3;
if (average >= 70)
letter = 'P';
else
letter = 'F';
return;
}
This seems very much like a exercise I just recently did and you can simplify this probably by using a vector to store the grades, then make use of the vectors sort functions to determine the median or average.
You could take it out. It doesn't do anything except make sure that any code placed after it doesn't get executed. There's nothing after though, so it's completely pointless.