Hi guys I am a beginner in C++ and just started using Class function. I have come up with a code but my average is incorrect and I am unable to figure out what is the mistake. Pls help mi guys ....
In your addRating(), you're collecting the count of that point. However in getAverage() you sum the total count and divide by 5. It's not calculating average. Check your algorithm.
Here is the original post. In future avoid deletin your posts. It prevents people who know how to use search from getting help quickly.
dev05 wrote:
Hi guys I am a beginner in C++ and just started using Class function. I have come up with a code but my average is incorrect and I am unable to figure out what is the mistake. Pls help mi guys ....
EDIT:
YOU HAVE DECLARED a local variable with the same name RESULT...Remove that declaration and your program will work
1 2 3 4 5 6 7 8 9 10 11 12 13 14
double StallRating :: getAverage()
{
double num = numRated[0]+numRated[1]+numRated[2]+numRated[3]+numRated[4];
double total = 5.0;
result = num/total; /// Local variable declaration removed
return result;
}
//just my 2 cents try to use loops when dealing with arrays its a lot nicer looking
// for example
for(int i = 0; i < NUMBER_OF_GRADES)
{
sum += grades[i];
}
Please DON'T delete your question after you've received your answer. It's extremely selfish - it deprives future readers of the opportunity to learn from this thread.
Interesting...How would that not cause a problem? He is storing all the info in to a variable local to that function and once out of scope that variable will be non existent..then he calls a display function that displays the other variable result