Assigning values to letters in a 2D character array

Hello,

what I have here is a program that reads letters (grades) from a 2D character array and out puts them into a row/column system that shows the grades of 3 subjects for 5 different students.

My next objective is to calculate and display the GPA of each student by adding the 3 grades per student and then dividing it by 3.

This requires that I assign a value to each grade from the array into the format of A = 4, B = 3, C = 2, D = 1, F = 0. But I am having trouble with figuring out how to do this.

Thank you for your help

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
// grades.txt

A
A
B
C
C
F
C
D
B
B
A
C
B
A
B

// main.cpp	

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	char grades[5][3];
	int ROW;
	int COL;
	
	// opens grades.txt
	ifstream inFile;
	inFile.open("grades.txt");

	if (inFile.fail())
	{
		cout << "Error! File did not open!... now closing...\n\n";
		exit(1);
	}
	
	cout << "All Grades\n" 
		 << "Student" << '\t' 
		 << "English" << '\t'
		 << "History" << '\t'
		 << "Math" << '\t' << '\n';

	// reads grades.txt into 2D array
	for (int ROW = 0; ROW < 5; ROW++)
	{
		for (int COL = 0; COL < 3; COL++)
		{
			inFile >> grades[ROW][COL];
		}
	}

	// outputs characters in 2D array
	for (int ROW = 0; ROW < 5; ROW++)
	{
		cout << "#" << ROW+1 << '\t';
		for (int COL = 0; COL < 3; COL++)
		{
			cout << grades[ROW][COL] << '\t';
		}
		cout << endl;
	}

	cout << endl;
	
	inFile.close();

	return 0;
}
closed account (j3Rz8vqX)
Option:
1
2
3
4
5
6
7
8
9
	// reads grades.txt into 2D array
	for (int ROW = 0; ROW < 5; ROW++)
	{
		for (int COL = 0; COL < 3; COL++)
		{
			inFile >> grades[ROW][COL];
                        grades[ROW][COL] = 4+'A'-grades[ROW][COL];//'A'-grades[ROW][COL] == (0 to -4) + 4 == (4 to 0)
		}
	}
Last edited on
Topic archived. No new replies allowed.