Need help with my program that computes average with a letter grade.

I only have two questions about what I have so far. My first question is whenever I run my program it works well but the Letter grade given at the end is always an F. How do I go about fixing that? My next question is that I need the fours tests to have different grade percentages. For instance, test1 counts as 20% of the grade, test 2 and test 3 are 25% each and test 4 is 30%. How do I go about calculating that instead of just adding all of them and dividing by four.
(Sorry if I mess up posting the code, it's my first time posting here)

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
  #include <iostream>

using namespace std;


void getScores(double&, double&, double&, double&);
double findAverage(double&, double&, double&, double&, double&);
char toLetterGrade(double);

int main()
{
    double test1 = 0.0;   
	double test2 = 0.0;    
	double test3 = 0.0;   
	double test4 = 0.0;
    double average = 0.0;
    
    cout << "This program calculates your class average." << endl << endl;
    
  
    getScores(test1, test2, test3, test4);
    
    
    cout << "Calculated Average: " << findAverage(test1, test2, test3, test4, average) << endl <<
            "Letter Grade: " << toLetterGrade(average) << endl;
    
   
    return 0;
}


void getScores(double& test1, double& test2, double& test3, double& test4)
{
    cout << "Enter first test score: ";
	cin >> test1;
	cout << "Enter second test score: ";
	cin >> test2;
	cout << "Enter third test score: ";
	cin >> test3;
	cout << "Enter fourth test score: ";
	cin >> test4;
}


double findAverage(double& test1, double& test2, double& test3, double& test4, double& average)
{
    average = (test1 + test2 + test3 + test4) / 4;
    
    return average;
}


char toLetterGrade(double average)
{
	char courseGrade;
	
if (average >= 90)
	{
		courseGrade = 'A';
	}
	else if (average >= 80 && average < 90)
	{
		courseGrade = 'B';
	}
	else if (average >= 70 && average < 80)
	{
		courseGrade = 'C';
	}
	else if (average >= 60 && average < 70)
	{
		courseGrade = 'D';
	}
	else if (average < 60)
	{
		courseGrade = 'F';
	}
	
    return courseGrade;
}
For calculating the % of how much each test is worth you have to add weight to each test.. err pts ..
Example:
Test1: = 20% of grade 20/20 pts
Test2: = 25% of grade 25/25 pts
Test3: = 25% of grade 25/25 pts
Test4: = 30% of grade 30/30 pts

Total: = 100% 100/100 pts
Grade: = A

John Dumb Scores:
Test1: = 20% of grade 7/20 pts
Test2: = 25% of grade 10/25 pts
Test3: = 25% of grade 12/25 pts
Test4: = 30% of grade 0/30 pts

Total: = 100% 29/100 pts = 29%
Grade: = F

Example with different pts system:
Marry Jane
Test1: = 20% of grade 78/80 pts
Test2: = 25% of grade 98/100 pts
Test3: = 25% of grade 70/100 pts
Test4: = 30% of grade 100/120 pts

Total: = 100% 346/400 pts = .865^ = 87%
Grade: = B

Hope that helps for your 2nd Question..


Okay I looked at your code and why don't you try this:

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
char toLetterGrade(double average)
{
	char courseGrade;


	if (average >= 90)
	{
		courseGrade = 'A';
	
	}
	if (average >= 80 && average < 90)
	{
		courseGrade = 'B';
	}
	if (average >= 70 && average < 80)
	{
		courseGrade = 'C';
	}
	if (average >= 60 && average < 70)
	{
		courseGrade = 'D';
	}
	if (average < 60)
	{
		courseGrade = 'F';
	}

	return courseGrade;
}







Last edited on
Would I have to add in something to the

1
2
3
4
5
6
7
double findAverage(double& test1, double& test2, double& test3, double& test4, double& average)
{

    average = (test1 + test2 + test3 + test4) / 4;
    
    return average;
}

?

If so what would I have to do? I've been working on it for 3 hours now and I'm lost at the moment :(.
Regarding my first question my output has the correct average of the four tests but still outputs an F, even though the Average is above 60. How would I go about fixing that?
For your first question if you output the average within the function toLetterGrade you will notice that its value is zero.thus it will always return F.
This code should work for you:
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
#include <iostream>

using namespace std;


void getScores(double&, double&, double&, double&);
void findAverage(double&, double&, double&, double&, double&);
char toLetterGrade(double);

int main()
{
	double test1 = 0.0;
	double test2 = 0.0;
	double test3 = 0.0;
	double test4 = 0.0;
	double average = 0.0;

	cout << "This program calculates your class average." << endl << endl;


	getScores(test1, test2, test3, test4);

	findAverage(test1, test2, test3, test4, average);

	cout << "Calculated Average: " << endl << average << endl <<
		"Letter Grade: " << toLetterGrade(average) << endl;

	int n; cin >> n;

	return 0;
}


void getScores(double& test1, double& test2, double& test3, double& test4)
{
	cout << "Enter first test score: ";
	cin >> test1;
	cout << "Enter second test score: ";
	cin >> test2;
	cout << "Enter third test score: ";
	cin >> test3;
	cout << "Enter fourth test score: ";
	cin >> test4;
}


void findAverage(double& test1, double& test2, double& test3, double& test4, double& average)
{
	average = (test1 + test2 + test3 + test4) / 4;
}


char toLetterGrade(double average)
{
	char courseGrade;

	if (average >= 90)
	{
		courseGrade = 'A';
	}
	else if (average >= 80 && average < 90)
	{
		courseGrade = 'B';
	}
	else if (average >= 70 && average < 80)
	{
		courseGrade = 'C';
	}
	else if (average >= 60 && average < 70)
	{
		courseGrade = 'D';
	}
	else if (average < 60)
	{
		courseGrade = 'F';
	}

	return courseGrade;
}
Thanks Konstance, I really appreciate it. Now to work on the second question
You should always when defining your Functions name the parameters.. it really helps make your code easier to read..
1
2
3
void getScores(double& testScore1, double& testScore2, double& testScore3, double& testScore4);
double findAverage(double& testAvg1, double& testAvg2, double& testAvg3, double& testAvg4, double& average);
char toLetterGrade(double average);


And You wouldn't be using average function anymore because each test has a weight to it.. you would have to upgrade your program..

for example not put in the % they did on the test1,2,3.. but put in what they had scored
John Dumb Scores:
Test1: = 20% of grade 7/20 pts

you would have to recalculate there grades based on points .. let me know if you still need help
Last edited on
Yeah I'm trying to go about doing that instead of just dividing all of them by four. I just cant seem to actually write the code out. I'm having trouble with functions. We just recently learned them in class.
Alright.. give me 10mins and I'll add a new function that should work!
Try this.. all I did was comment old stuff out and created some new functions.. compare the functions so you can see what I did to change it!

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
113
114
115
116
#include <iostream>

using namespace std;


void getScores(double& test1, double& test2, double& test3, double& test4);
void findAverage(double& test1, double& test2, double& test3, double& test4, double& average);
char toLetterGrade(double average);
//NEW STUFF
void WelcomeMsg();
void GetScores2(double& test1, double& test2, double& test3, double& test4);
void FindAverage2(double& test1, double& test2, double& test3, double& test4, double& average);

int main()
{
	double test1 = 0.0;
	double test2 = 0.0;
	double test3 = 0.0;
	double test4 = 0.0;
	double average = 0.0;

	//cout << "This program calculates your class average." << endl << endl;

	//NEW STUFF!!!!!!!!!!!!!
	WelcomeMsg();
	GetScores2(test1, test2, test3, test4);
	FindAverage2(test1, test2, test3, test4, average);


	//getScores(test1, test2, test3, test4);
	//findAverage(test1, test2, test3, test4, average);

	cout << "" << endl;
	cout << "Calculated Average: " << endl;
	cout << "==================" << endl;
	cout << average << "%" << endl;
	cout << "Letter Grade: " << toLetterGrade(average) << endl;

	int n; cin >> n;

	return 0;
}


void getScores(double& test1, double& test2, double& test3, double& test4)
{
	cout << "Enter first test score: ";
	cin >> test1;
	cout << "Enter second test score: ";
	cin >> test2;
	cout << "Enter third test score: ";
	cin >> test3;
	cout << "Enter fourth test score: ";
	cin >> test4;
}


void findAverage(double& test1, double& test2, double& test3, double& test4, double& average)
{
	average = (test1 + test2 + test3 + test4) / 4;
}


char toLetterGrade(double average)
{
	char courseGrade;

	if (average >= 90)
	{
		courseGrade = 'A';
	}
	else if (average >= 80 && average < 90)
	{
		courseGrade = 'B';
	}
	else if (average >= 70 && average < 80)
	{
		courseGrade = 'C';
	}
	else if (average >= 60 && average < 70)
	{
		courseGrade = 'D';
	}
	else if (average < 60)
	{
		courseGrade = 'F';
	}

	return courseGrade;
}

void WelcomeMsg()
{
	cout << "Welcome to class grade Program!" << endl;
	cout << "to figure out your class grade please" << endl;
	cout << "enter your score on each of the 4 tests \n" << endl;
}

void GetScores2(double& test1, double& test2, double& test3, double& test4)
{
	cout << "What did you score on Test1? Possible pts: 80/80 Grade Weight: 20%\n" << endl;
	cin >> test1;
	cout << "What did you score on Test2? Possible pts: 100/100 Grade Weight: 25%\n" << endl;
	cin >> test2;
	cout << "What did you score on Test3? Possible pts: 100/100 Grade Weight: 25%\n" << endl;
	cin >> test3;
	cout << "What did you score on Test4? Possible pts: 120/120 Grade Weight: 30%\n" << endl;
	cin >> test4;
}

void FindAverage2(double& test1, double& test2, double& test3, double& test4, double& average)
{
	average = ((test1 + test2 + test3 + test4) / 400) * 100;
	// prob could modulate the average here also so you don't return crazy decimals places..

}
Holy C**p thanks so much. You have no idea how much you just helped me. I REALLY appreciate it. Yer a wizard.
Topic archived. No new replies allowed.