Function Problem

When I run my program, everything seems to work fine except for the function getLetterGrade. The letter grade is not being displayed at the end. Also, getLetterGrade has to be a void function where totalAvg is passed by value and letterGrade is passed by reference. What am I doing wrong?

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <iomanip>
using namespace std;

double averageGrade();
double classAverage(double labAvg, double testAvg, double projectAvg);
void getLetterGrade(double totalAvg, char &letterGrade);
void displayGradeReport(double labAvg, double testAvg, double projectAvg, double totalAvg, char letterGrade);

int main()
{
	//Variables
	double average = 0.0;
	double labAvg = 0.0;
	double testAvg = 0.0;
	double projectAvg = 0.0;
	double totalAvg = 0.0;
	char letterGrade = ' ';
	char yesNo = ' ';

	do
	{
		//Input Section
		cout << "Enter the student's lab grades below." << endl << endl;
		average = averageGrade();
		labAvg = average;

		cout << "Enter the student's test grades below." << endl << endl;
		average = averageGrade();
		testAvg = average;

		cout << "Enter the student's project grades below." << endl << endl;
		average = averageGrade();
		projectAvg = average;

		totalAvg = classAverage(labAvg, testAvg, projectAvg);
		cout << endl;

		//Display Section
		displayGradeReport(labAvg, testAvg, projectAvg, totalAvg, letterGrade);

		cout << "Do you want to enter data for a new student? (Y/N): ";
		cin >> yesNo;
		yesNo = toupper(yesNo);
				
	} while (yesNo == 'Y');
	
	cout << endl;
	system("pause");
	return 0;
}

double averageGrade()
{
	double grade = 0.0;
	double gradeTotal = 0.0;
	int totalWorks = 0;
	double average = 0.0;

	cout << "Enter the first grade (-1 to exit): ";
	cin >> grade;
	while (grade != -1)
	{
		gradeTotal += grade;
		totalWorks++;
		cout << "Enter the next grade (-1 to exit): ";
		cin >> grade;
	}
	cout << endl;
	average = gradeTotal / totalWorks; 
	return average;
}

double classAverage(double labAvg, double testAvg, double projectAvg)
{
	double totalAvg = 0.0;
	labAvg *= 0.5;
	testAvg *= 0.4;
	projectAvg *= 0.1;
	totalAvg = labAvg + testAvg + projectAvg; 
	return totalAvg;
}

void getLetterGrade(double totalAvg, char &letterGrade)
{
	if (totalAvg >= 90)
	{
		char letterGrade = 'A';
	}
	else if (totalAvg < 90 && totalAvg >= 80)
	{
		char letterGrade = 'B';
	}
	else if (totalAvg < 80 && totalAvg >= 70)
	{
		char letterGrade = 'C';
	}
	else if (totalAvg < 70 && totalAvg >= 60)
	{
		char letterGrade = 'F';
	}
}

void displayGradeReport(double labAvg, double testAvg, double projectAvg, double totalAvg, char letterGrade)
{
	cout << "Lab average: " << labAvg << endl;
	cout << "Test average: " << testAvg << endl;
	cout << "Project average: " << projectAvg << endl;
	cout << "Class average: " << totalAvg << endl;
	cout << "Letter grade: " << letterGrade << endl;
	cout << endl;
}
you aren't calling the getlettergrade function
simply insert this code on line 39

 
getLetterGrade( totalAvg,letterGrade);


i also realize that your getLetterGrade will not work if totalAvg is less than
60, you should check that too.
Last edited on
Okay, thanks for the help, I'll fix totalAvg. However, I added that code to line 39 and it is still not displaying the letter grade, its just showing up blank in the command window. letterGrade has to be displayed through the function displayGradeReport per my homework instructions. :(
sorry, there is a problem in your getLetterGrade i see.

in all those if, remove all those char whatever, you are creating new local variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void getLetterGrade(double totalAvg, char &letterGrade)
{
	if (totalAvg >= 90)
	{
		letterGrade = 'A';
	}
	else if (totalAvg < 90 && totalAvg >= 80)
	{
		letterGrade = 'B';
	}
	else if (totalAvg < 80 && totalAvg >= 70)
	{
		letterGrade = 'C';
	}
	else if (totalAvg < 70 && totalAvg >= 60)
	{
		letterGrade = 'F';
	}
}


should be like that.
Last edited on
Yep, that was it. I can't believe I didn't notice that before. I'm way too tired to be doing programming right now, haha. XD

Thank you!
Anytime lol, you should also ask yourself what happens when totalAv < 60
Topic archived. No new replies allowed.