If I understand your code
- you’ve created a class “Student”.
- You read Student’s data from a file into std::vectors which are not members of Student.
- You need to perform some basic calculation on those data.
For what I can see, there could be a misunderstanding: all the data you’re dealing with were probably supposed to be managed from inside your class Student - otherwise I can’t see the point of writing that class.
Let’s assume the data you need to manage are (for each Student):
a) first and last name;
b) list of homework marks;
c) list of exam score;
d) final exam score.
It seams you’re putting those data into:
a) vector<string> names; --> a std::vector for all Students names
b) vector<vector<int>> hw; --> a std::vector of std::vectors for all Students lists of homework marks
c) vector<vector<int>> e; --> a std::vector of std::vectors for all Students lists of exam scores
d) vector<int> f; --> a std::vector for all Students final exam scores
While it is possible the assignment asks you to use your class more or less this way:
1 2 3 4 5 6 7 8 9 10 11 12
|
#ifndef SHIMSHIM96_STUDENT_H
#define SHIMSHIM96_STUDENT_H
class Student {
std::string fname, lname;
std::vector<int> hws;
std::vector<int> exams;
int fscore;
double average;
};
#endif // SHIMSHIM96_STUDENT_H
|
Maybe I’m wrong, of course, but I’d read the assignment again to be sure.
Anyway, about your question
I need to add something that removes the lowest homework score |
Once you’ve sorted your std::vector, the lowest homework mark is the first one, i.e. the one at position 0.
I think your "student_data.dat" file is somewhat like:
Ann Anderson
13 666 679
51 34 47
18
Barbara Brown
14 667 681
52 74 16
44
Carl Carter
56 28 45
63 11 26
85
Let’s reduce it to:
Ann
13 666 679
Barbara
14 667 681
Carl
56 28 45
to deal only with homework marks.
An example of code similar to yours could be:
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
|
#include <algorithm>
#include <fstream>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
std::vector<int> process_line(const std::string& line);
double getAverage(std::vector<int>& hws);
int getRowSum(const std::vector<int>& hws);
int main()
{
std::ifstream input_file("student_data.dat");
if (! input_file) {
std::cout << "The file could not be opened!\n";
return 0;
}
std::vector<std::string> names;
std::vector<std::vector<int>> hw;
int linenum { 1 };
for(std::string line; std::getline(input_file, line); ++linenum) {
if(line.empty()) { linenum = 0; }
switch (linenum) {
case 0:
break;
case 1:
names.push_back(line);
break;
case 2:
hw.push_back(process_line(line));
break;
}
}
input_file.close();
std::vector<double> averages(names.size());
for(size_t i{}; i<hw.size(); ++i) { averages.at(i) = getAverage(hw.at(i)); }
for (size_t i=0; i<names.size(); ++i) {
std::cout << names.at(i) << " average is " << averages.at(i) << '\n';
}
return 0;
}
std::vector<int> process_line(const std::string& line)
{
std::istringstream iss(line);
std::vector<int> entries;
for(int i{}; iss >> i; /**/) { entries.push_back(i); }
return entries;
}
double getAverage(std::vector<int>& hws)
{
std::sort(hws.begin(), hws.end());
return static_cast<double>(getRowSum(hws) - hws.at(0)) / (hws.size() - 1);
}
int getRowSum(const std::vector<int>& hws)
{
return std::accumulate(hws.begin(), hws.end(), 0);
}
|
Output:
Ann average is 672.5
Barbara average is 674
Carl average is 50.5 |