I'm supposed to build a grade calculator using weights from different types of work (Lab, project and test), and for some reason I can't get it to work. It keeps giving me -2 as an answer for the average. I'm assuming I'm passing variables incorrectly, and the tutorial on this site, nor my textbook seem to be helping me.
I know there's a lot of unnecessary stuff right now, like extra prototype functions, but I plan on adding more later once I figure out how to call functions properly.
No essentially, the function is supposed to take the grade entered, add them altogether, then average it. I was using oldGrade in order to keep a running total of the grade that way I could use the average = oldGrade / totalWorks to return the average. Honestly, newGrade probably would have made more sense, but I've switched variables and such around so much trying to debug that it's a bit of a mess.
If I substituted your code into the do while loop, wouldn't that change the average each iteration of the loop to where the cout of the average would simply be based off the last loop's averageGrade function result?
This would change the last loop average. The function though keeps on resetting oldGrade to 0.0. Now I know you would then state double oldGrade; This would result in oldGrade to be reinitialized with a garbage number. The best option is have the do loop get all the grades, count the totalWorks after then call the function after the loop:
int totalWorks = 0;
double AllGrades;
do
{
cin >> grade;
AllGrades += grade;
totalWorks++;
} while (grade != -1);
average = averageGrade(AllGrades, totalWorks);
NOTE: average is not needed for function input. totalWorks++ is not needed within the function.
3 IntelliSense: more than one instance of overloaded function "averageGrade" matches the argument list:
function "averageGrade(double &average, double &totalWorks, double &grade)"
function "averageGrade(double grade, double totalWorks, double average)"
argument types are: (double, double, double) \GradeProgram\GradeProgram\GradeProgram.cpp 33 3 GradeProgram
That works perfectly, so obviously I was just plugging it in wrong. Thank you very much, the way you wrote it showed me my mistakes.
I was definitely passing stuff wrong, as well as trying to lump far too much in the function.
I may end up having to recluster some things in the function however that way I won't have to put the allgrades += grade; and totalWorks++ each time when I'm trying to average different project types, but I think I can work that out.
Ugh, so I finally got home and capable of using my compiler and looking at the instructions again and I realized why I had all that nested within the averageGrade function to begin with. It states the averageGrade function has to have a do while loop, sentinel and priming value, and the ability to enter grades.