Why Doesn't This If Else Statement Work?

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.
I flipped the less than sign to a greater than, and letter is still always assigned P no matter what...
So your code looks like this now?

1
2
3
4
5
6
7
8
9
10
//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;
}
Yes, Sorry, I was trying to switch 'P' and 'F' to see if that was part of it and I didn't flip back the sign...

I'm just not sure what's wrong, I just know that something is.
Could it be because you spelled "judgment" wrong? :P
closed account (3qX21hU5)
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.
Last edited on
The code I posted above works fine. If your code looks like that, it's working.
I could swear that void doesn't return anything :S
I could swear that void doesn't return anything :S


It doesn't. That's why the function doesn't return anything.
If i could see all of your code then maybe i could help you out
but then what is the point of return;?

couldn't you just take that out?
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.