How to turn grade average into a Letter grade.

Hello I am new to C++, and I need some help with my assignment. We are learning about arrays in my class. We have an assignment where we have to create a Grade calculator for a teacher to input 4 test scores for 5 students. The teacher inputs each students name and test scores, and the program is supposed to take those 4 test scores and give an average, and a letter grade for each student. The students NAME, AVERAGE, and LETTER GRADE should print out. So far I am able to get the students name and average to print out. but I cant get the Letter grade to come out. Could someone help point me to the right direction? here is my code...

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;


//Function prototype
void score();
void get_grades();



//Arrays 
string name;
const int students = 5;
string names[students];
char grades[students] = { 'A', 'B','C','D','F' };
int test1[students];
int test2[students];
int test3[students];
int test4[students];
float average[students];


// Variables for Menu function
int input;
string title = "Student Grade Program";
string guide = "Choose from the following menu";
string run = " 1.) Enter Student Information";
string quit = " 2.) Quit the Program";
const int run_choice = 1;
const int quit_choice = 2;
string quitmsg = "You have Quit the program";
string defaultmsg = "Please input a valid selection. Only choices 1 and 2 are valid";

//Variables for test score input
string question1 = "Enter the student's name";
string question2 = "Enter the first test score ";
string question3 = "Enter the second test score ";
string question4 = "Enter the third test score ";
string question5 = "Enter the fourth test score ";
string colon = ": ";


// Menu Function
void menu()
{
	do
	{
		cout << title << endl
			<< guide << endl
			<< run << endl
			<< quit << endl;
		cin >> input;
		switch (input)
		{
		case run_choice:
		{
			score();
			break;
		}
		case quit_choice:
		{
			cout << quitmsg << endl << endl;
			break;
		}
		default:
		{
			cout << defaultmsg << endl << endl;
		}

		}
	} while (input != quit_choice);

}


// test score input function
void score()
{
	for (int i = 0; i < students; i++)
	{
		cout << question1 << endl;
		cin >> names[i];

		cout << question2 << colon << endl;
		cin >> test1[i];

		cout << question3 << colon << endl;
		cin >> test2[i];

		cout << question4 << colon << endl;
		cin >> test3[i];

		cout << question5 << colon << endl;
		cin >> test4[i];
	}
	
	cout << "Student" << "\t\t" << "Average" << "\t\t" << "Letter Grade" << endl;
	
	for (int i = 0; i < students; i++)
	{
		average[i] = (test1[i] + test2[i] + test3[i] + test4[i]) / 4;
	
	}

	for (int i = 0; i < students; i++)
	{
		cout << names[i] << "\t\t" << average[i] << "\t\t" << get_grades << endl;
	}

}

void get_grades()
{
	for (int i = 0; i < students; i++)
	{

		if (average[i] >= 90)
			cout << grades[0];
		else if (average[i] >= 80 && average[i] < 90)
			cout << grades[1];
		else if (average[i] >= 70 && average[i] < 80)
			cout << grades[2];
		else if (average[i] >= 60 && average[i] < 70)
			cout << grades[3];
		else
			cout << grades[4];
	}
}



// Main program

int main()
{
	menu();
	return 0;
}
Last edited on
The problem is that get_grades is void function, so it doesn't return anything, and you can't pass it in cout. So you can make it char type to return A, B, C, D or E, something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
char get_grades(float points)
{
     if (points >= 90)
			return grades[0];
		else if (points >= 80 && points < 90)
			return grades[1];
		else if (points >= 70 && points < 80)
			return grades[2];
		else if (points >= 60 && points < 70)
			return grades[3];
		else
			return grades[4];
}


and then you can pass it with average[i] as function argument, so it will look like 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;


//Function prototype
void score();
char get_grades(float);



//Arrays 
string name;
const int students = 5;
string names[students];
char grades[students] = { 'A', 'B','C','D','F' };
int test1[students];
int test2[students];
int test3[students];
int test4[students];
float average[students];


// Variables for Menu function
int input;
string title = "Student Grade Program";
string guide = "Choose from the following menu";
string run = " 1.) Enter Student Information";
string quit = " 2.) Quit the Program";
const int run_choice = 1;
const int quit_choice = 2;
string quitmsg = "You have Quit the program";
string defaultmsg = "Please input a valid selection. Only choices 1 and 2 are valid";

//Variables for test score input
string question1 = "Enter the student's name";
string question2 = "Enter the first test score ";
string question3 = "Enter the second test score ";
string question4 = "Enter the third test score ";
string question5 = "Enter the fourth test score ";
string colon = ": ";


// Menu Function
void menu()
{
	do
	{
		cout << title << endl
			<< guide << endl
			<< run << endl
			<< quit << endl;
		cin >> input;
		switch (input)
		{
		case run_choice:
		{
			score();
			break;
		}
		case quit_choice:
		{
			cout << quitmsg << endl << endl;
			break;
		}
		default:
		{
			cout << defaultmsg << endl << endl;
		}

		}
	} while (input != quit_choice);

}


// test score input function
void score()
{
	for (int i = 0; i < students; i++)
	{
		cout << question1 << endl;
		cin >> names[i];

		cout << question2 << colon << endl;
		cin >> test1[i];

		cout << question3 << colon << endl;
		cin >> test2[i];

		cout << question4 << colon << endl;
		cin >> test3[i];

		cout << question5 << colon << endl;
		cin >> test4[i];
	}
	
	cout << "Student" << "\t\t" << "Average" << "\t\t" << "Letter Grade" << endl;
	
	for (int i = 0; i < students; i++)
	{
		average[i] = (test1[i] + test2[i] + test3[i] + test4[i]) / 4;
	
	}

	for (int i = 0; i < students; i++)
	{
		cout << names[i] << "\t\t" << average[i] << "\t\t" << get_grades(average[i]) << endl;
	}

}

char get_grades(float points)
{
     if (points >= 90)
			return grades[0];
		else if (points >= 80 && points < 90)
			return grades[1];
		else if (points >= 70 && points < 80)
			return grades[2];
		else if (points >= 60 && points < 70)
			return grades[3];
		else
			return grades[4];
}



// Main program

int main()
{
	menu();
	return 0;
}


Also I want to note few things:
1. Why did you make grades array with size students? There are only five grades, but number of students can be any number you put (I mean this from logic side, it is correct if number of students is 5, like in your problem)
It should be declared and initialized in this way:
char grades[5]={'A', 'B', 'C', 'D', 'E'};
2. Why so global arrays and variables? It is not good practice to make variables global in C++
Last edited on
Topic archived. No new replies allowed.