I am going through Accelerated C++ using Visual C++ express 2010. In chapter 4 it makes a struct for student info and supposedly reads names and marks in from a file, and this is where I am stumped. For those who don't have the book, the code is
Code:
#ifndef GUARD_Student_info
#define GUARD_Student_info
#include <iostream>
#include <string>
#include <vector>
struct Student_info {
std::string name;
double midterm, final;
std::vector<double> homework;
};
bool compare(const Student_info&, const Student_info&);
std::istream& read(std::istream&, Student_info&);
std::istream& read_hw(std::istream&, std::vector<double>&);
#endif
Code:
#include "Student_info.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)
{
is >> s.name >> s.midterm >> s.final;
read_hw(is,s.homework); //read and store all the students' homework grades
return is;
}
istream& read_hw(istream& in, vector<double>& hw)
{
if(in)
{
//get rid of previous contents
hw.clear();
//read homework grades
double x;
while(in>>x)
hw.push_back(x);
//clear the stream so that the input would work for the next student
in.clear();
}
return in;
}
Code:
#ifndef GUARD_grade_h
#define GUARD_grade_h
#include <vector>
#include "Student_info.h"
double grade(double, double, double);
double grade(double, double, const std::vector<double>&);
double grade(const Student_info&);
#endif
Code:
#include <stdexcept>
#include <vector>
#include "grade.h"
#include "median.h"
#include "Student_info.h"
using namespace std;
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);
}
Code:
#ifndef GUARD_median_h
#define GUARD_median_h
#include <vector>
double median(std::vector<double>);
#endif
Code:
#include "median.h"
#include <vector>
#include <algorithm>
using std::istream;
using std::vector;
using std::sort;
using std::domain_error;
double median(std::vector<double> vec)
{
typedef vector<double>::size_type vec_sz;
vec_sz size = vec.size();
if (size==0)
throw domain_error("median of an empty vector");
sort(vec.begin(), vec.end());
vec_sz mid = size/2;
return size % 2==0 ? (vec[mid] + vec[mid-1]) /2 : vec[mid];
}
and the main class
Code:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <ios>
#include <stdexcept>
#include <string>
#include <vector>
#include "grade.h"
#include "Student_info.h"
using std::cout;
using std::cin;
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;
int main()
{
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0;
while (read(cin, record)) {
maxlen = max(maxlen, record.name.size());
students.push_back(record);
cout << "entered";
cout << students[0].name << string(maxlen+1 - students[0].name.size(), ' ');
}
sort(students.begin(), students.end(), compare);
for(vector<Student_info>::size_type i=0;
i != students.size(); ++i) {
cout << students[i].name << string(maxlen+1 - students[i].name.size(), ' ');
try {
double final_grade = grade(students[i]);
streamsize prec = cout.precision();
cout << setprecision(3) << final_grade << setprecision(prec);
} catch (domain_error e) {
cout << e.what();
}
cout << endl;
}
cin.ignore();
cin.get();
return 0;
}
Initially I get a blank screen, I can input some values but no way of printing results out.