Problem with reading from a file

I need to read a set of data from a file into a vector.First column contains the names of the student and other two contain their grades. Then from a vector I need to calculate which student have passed using function grade. This is one of the tasks from Accelerated C++. I assume I had made a mistake in read function.

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
// main.cpp
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include <ctime>
#include <fstream>
#include "grade.h"
#include "Student_info.h"

using std::cin;
using std::cout;
using std::domain_error;
using std::endl;
using std::max;
using std::setprecision;
using std::sort;
using std::streamsize;
using std::string;
using std::vector;
using std::ifstream;

int main()
{
	ifstream file;
	file.open("10000.txt");
	vector<Student_info> students;
    Student_info record;
	while (read(file, record)) {
        students.push_back(record);
    }
	file.close();
    std::clock_t start = clock();
	vector<Student_info> fails = extract_fails(students);
	std::clock_t ends = clock();
	cout << "Time taken to extract failing grades: " << (double)(ends - start) / CLOCKS_PER_SEC << endl;
    return 0;
}


1
2
3
4
5
6
//Student_info.h
struct Student_info {
	std::string name;
	double midterm, final;
	std::vector<double> homework;
};


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
//Student_info.cpp
#include "Student_info.h"
#include "grade.h"

using std::istream; using std::vector;

bool compare(const Student_info& x, const Student_info& y)
{
	return x.name < y.name;
}

istream& read(istream& is, Student_info& s)
{
	// read and store the student's name and midterm and final exam grades
    is >> s.name >> s.midterm >> s.final;
    read_hw(is, s.homework); // read and store all the student's homework grades
    return is;
}

istream& read_hw(istream& in, vector<double>& hw)
{
	if (in) 
	{
		// get rid of previous content
		hw.clear();
		// read homework grades
		double x;
		while (in >> x)
			hw.push_back(x);
		// clear the stream so that input will work for the next student
		in.clear();
	}
	return in;
}

vector<Student_info> extract_fails(vector<Student_info>& students)
{
	vector<Student_info> fail;
	vector<Student_info>::iterator iter = students.begin();
	while (iter != students.end()) {
		if (fgrade(*iter)) {
			fail.push_back(*iter);
			iter = students.erase(iter);
		} else
			++iter;
	}
	return fail;
}


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
//grade.cpp
#include <stdexcept>
#include <vector>
#include "grade.h"
#include "median.h"
#include "Student_info.h"

using std::domain_error;
using std::vector;

double grade(double midterm, double final, double homework)
{
       return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}

double grade(double midterm, double final, const vector<double>& hw)
{
       if (hw.size() == 0)
           throw domain_error("student has done no homework");
       return grade(midterm, final, median(hw));
}

double grade(const Student_info& s)
{
       return grade(s.midterm, s.final, s.homework);
}

bool fgrade(const Student_info& s)
{
	return grade(s) < 60;
}

double grade(const std::vector<Student_info>::iterator& iter) 
{
	return grade((*iter).midterm, (*iter).final, (*iter).homework);
}

Last edited on
Please post a small sample of your input file.
Topic archived. No new replies allowed.