Another calling function issue and calling by reference.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

//Prototype Functions

double averageGrade(double& average, double& totalWorks, double& grade);
double classAverage(double& testAverage, double& labAverage, double& projectAverage);
char getLetterGrade(char& finalGrade);
string displayGradeReport(string& midtermReport);


int main()
{
	//Variables
	double grade = 0.0;
	double average = 0.0;
	double testAverage = 0.0;
	double labAverage = 0.0;
	double projectAverage = 0.0;
	char finalGrade = ' ';
	string midtermReport = " ";
	double totalWorks = 1.0;

	//Meat of the program.
	cout << "Please enter the student's first lab grade or -1 to exit and view the lab average: ";
	do {
		cin >> grade;
		averageGrade(grade, totalWorks, average);
		cout << "Please enter the grade for lab assignt #" << totalWorks << ": ";
	} while (grade != -1);
	totalWorks = 0;
	cout << endl << "This student's lab average is: " << average;
	system("pause");
	return 0;
}

double averageGrade(double& grade, double& totalWorks, double& average)
{
	double oldGrade = 0.0;

	totalWorks++;
	oldGrade = oldGrade + grade;
	average = oldGrade / totalWorks;
	return average;
}
I'm going to try something here. Within your do loop, what if you tried this:
average = averageGrade(grade, totalWorks, average);

Also, is the oldGrade within your function suppose to be the average? Or all of the other grades added together?

p.s. I would also try average += averageGrade(grade, totalWorks, average);
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.
Those aren't working, but quite frankly, I'm not sure I'm plugging that in right.

Is there anyway you could alter the source code displayed so I could see what you mean?
One last thing I could think of before anything is to try removing all the "&" within the function below.
That yields error:

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
I redid the part of code you were stuck on. Here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>

using namespace std;

double averageGrade(double allGrades, int totalWorks)
{
    double average = allGrades / totalWorks;
    return average;
}

int main()
{
    double grade, average, testAverage, labAverage, projectAverage;
    double allGrades = 0.0;
    int totalWorks = 0;
    char finalGrade;
    string midtermReport;
    cout << "Please enter the student's first lab grade";
    cout << "or -1 to exit and view the lab average: ";
    cin >> grade;
    while(grade != -1)
    {
        allGrades += grade;
        totalWorks++;
        cout << "Next grade: ";
        cin >> grade;
    }
    average = averageGrade(allGrades, totalWorks);
    cout << "Total grade is: " << average << endl;

    return 0;

}
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.

I appreciate your help.
No problem ^.^
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.
Topic archived. No new replies allowed.