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.
// 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>
usingnamespace 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:
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.