Create a Class to Calculate Students' Average Grades

Thanks to the gracious help of a community member last week, I was able to complete a program that involved reading in a list of 40 students and 7 grades for each student, and find the average grade for each student. This week, my assignment is to modify the program so that I use a class called "Student" to find the averages. Below is the code for the program I did last week.

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
  // Week 13 Program.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;

struct Studentinfo//information for each student
{
	string name;
	vector<int> grades;
};

vector<Studentinfo> studentsfromfile(string& filename, int assignments)
{
	vector<Studentinfo> allstudents;
	ifstream gradebook(filename);//opens input file
	string file;

	Studentinfo student;
	student.grades.resize(assignments);//resizes the struct to make it large enough to contain all of the grades

	for (int i = 0; gradebook >> student.name; ++i)//for each name read in from the file
	{
		for (int j = 0; j < assignments; j++)//read in the grades read for each name from the file
			gradebook >> student.grades[j];
		if (gradebook)
			allstudents.push_back(student);//adds succesfully read in data to the vector
	}
	return allstudents;//allows the vector to return a value when called in a different function
}

int main()
{
	string filename = "Gradebook.txt";
	int N_ASSIGNMENTS = 7;
	ifstream gradebook(filename);
	auto student_infos = studentsfromfile(filename, N_ASSIGNMENTS);//takes the vector from the above function and puts it into main, auto allows the compiler to create the type

	for (auto& sinfo : student_infos)
	{
		cout << setprecision(3) << "The average grade for  " << setw(10) << sinfo.name << " " << "is: ";
		int i = 7;
		double sum = 0.0, average = 0.0;
		for (int grade : sinfo.grades)
			sum += grade;//adds all 7 grades from each test together
		average = sum / i;//finds the average of all 7 grades for each student
		cout << average << endl;
	}
	return(0);
}



My question is what components of this would I need to take in order to properly put them into the class?
Currently my class.cpp looks 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
#include "pch.h"
#include "Student.h"
#include <string>

Student::Student()
{
}


Student::~Student()
{
}
void Student::getName(string name)
{

}
void Student::setName(string name)
{

}
void Student::setgrade1(int grade1)
{

}
void Student::setgrade2(int grade2)
{

}
void Student::setgrade3(int grade3)
{

}
void Student::setgrade4(int grade4)
{

}
void Student::setgrade5(int grade5)
{

}
void Student::setgrade6(int grade6)
{

}
void Student::setgrade7(int grade7)
{

}


And my header file looks 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
#pragma once
#include <string>
#include <iostream>
#include <vector>
using namespace std;
class Student
{
private:
	string name;
	int grade1, grade2, grade3, grade4, grade5, grade6, grade7;
public:
	Student();
	~Student();
	void getName(string name);
	void setName(string name);
	void setgrade1(int grade1);
	void setgrade2(int grade2);
	void setgrade3(int grade3);
	void setgrade4(int grade4);
	void setgrade5(int grade5);
	void setgrade6(int grade6);
	void setgrade7(int grade7);
};


I figure I need to take the vector function from the original program, but I'm not sure how to exactly extract the names and grades appropriately to do what I need. Thanks in advanced for any help.
Modify the Week 13 Program to use a class called Student which has a name and 7 grades. These should be private, and you should provide a getName() public function to access the name.

The class should have functions to enter the grades (1 thru 7) and to return the average.

Do all the printing in main

This is the specific assignment.
Topic archived. No new replies allowed.