Converting a char array into a an int holding value

Hi guys, I have a homework where I create a file and place grades such as A B C D and F; meant to stand for the students grades. I create a program where I read the values from the file and store them into a 2D array.
So far I have done that part, but then I am suppose to calculate the GPA of the student's grades.
I don't know how to change my char array from holding 'A' to holding the value 4.

Here's my code so far:

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
#include <iostream>
#include <fstream>
using namespace std;

const int ROWS = 5;
const int COLS = 3;

void showGrades(char studentsGrades[][COLS]);

int main()
{
	char studentsGrades[ROWS][COLS];
	
	showGrades(studentsGrades);

	return 0;
}
void showGrades(char studentsGrades[][COLS])
{
	ifstream inFile;

	inFile.open("C:\\Users\\Karen\\Documents\\Visual Studio 2013\\Projects\\HW2\\HW2\\grades.dat");
	
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			inFile >> studentsGrades[i][j];
		}
	}
	cout << "All grades: \n\n";
	cout << "Student \t" << "English \t" << "History \t" << "Math \n";

	for (int i = 0; i < ROWS; i++)
	{
		cout <<"#" << i+1 ;
		for (int j = 0; j < COLS; j++)
		{
			cout <<"\t\t"<< studentsGrades[i][j];
		}
		cout << endl;
	}
}

Output:
All grades:

Student         English         History         Math
#1              A               A               B
#2              C               C               F
#3              C               D               B
#4              B               A               C
#5              B               A               B
Press any key to continue . . .
	
Last edited on
A simple way would just be to have variable to accumulate the gpa and use a switch on the extracted letter grade. Case 'A' would add 4 to the accumulator, etc. Then average the accumulation. Do that for each student. I.E. keep an array of floats equal to the amount of students then add to the corresponding element.

I personally would go a totally different route and use a more OO approach, but I'm assuming this is homework so I'll leave it at that. Hope I helped.
Sorry i quite don't understand, can you break it down in code or something please.

because what I thought was getting an float variable making it = to students 1 array, then idk I got lost lol

Im guessing it has to be in a for loop like this, to get each value of the student
1
2
3
4
5
6
7
for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			inFile >> studentsGrades[i][j];
		}
	}


OR
should I be doing this?

studentsGrades[0][0] + studentsGrades[0][1] +studentsGrades[0][2];
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
float accumulator = 0.0f;
float studentGPAs[*]; //* represents number of students

//...

//do for each student;

for(int i = 0; i < *; i++) //* represents number of students
{
    for(int i = 0; i < *; i ++) //* represents however many grades a student can have
    {
        switch(c) //char extracted from file
        {
            case 'A':
            case 'a':
                accumulator += 4.0f;
                break;
           //do same for 'B', 'C', 'D', 'F'
        }
    }
    
    studentGPA[i] = accumulator / *; //* represents number of grades.

    accumulator = 0.0f; //set the accumulator back to 0.0f so we start fresh for the new student
}



Note that you can only switch on an integral type. So make sure that whatever you extract is a single character.
Last edited on
Topic archived. No new replies allowed.