I have attempted the source code from Chapter 4 in Accelerated C++ page 70 but keep getting the above error. I'm not sure whether it is in the way I have saved the header files or because my structure is wrong all together. I have included the code.
I have created 3 header files, 3 extra source files and the main source file. I have attached all of them. Please let me know how and where I should save these files to get the programme to function.
Many Thanks
Lannon
main.cpp
Expand|Select|Wrap|Line Numbers
- #include <algorithm>
- #include <iomanip>
- #include <ios>
- #include <vector>
- #include <string>
- #include <stdexcept>
- #include <iostream>
- #include "grade.h"
- #include "Student_info.h"
- using std::cin; using std::cout; using std::endl;
- using std::setprecision; using std::sort; using std::vector;
- using std::max; using std::domain_error; using std::string;
- using std::streamsize;
- int main()
- {
- vector<Student_info> students;
- Student_info record;
- string::size_type maxlen = 0; // LENGTH OF THE LONGEST NAME
- // read and store all the students data
- // Invariant: students contains all the students records read so far
- // maxlen contains the length of the longest name in students
- while (read(cin, record)) {
- // find the length of the longest name
- maxlen = max(maxlen, record.name.size());
- students.push_back(record);
- }
- // alphabetize the student records
- sort(students.begin(), students.end(), compare);
- // write the names and grades
- for (vector<Student_info>::size_type i = 0; i!= students.size(); ++i) {
- // write the name padded to the right by maxlen + 1 characters
- cout << students[i].name
- << string(maxlen + 1 - students[i].name.size(), ' ')
- // compute and write the grade
- try {
- double final_grade = grade(students[i]);
- streamsize prec = cout.precision();
- cout << setprecision(3) << final_grade
- << steprecision(prec);
- } catch (domain_error e) {
- cout << e.what();
- }
- cout << endl;
- }
- return 0;
- }
Expand|Select|Wrap|Line Numbers
- #ifndef GUARD_grade_h
- #define GUARD_grade_h
- // 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
Expand|Select|Wrap|Line Numbers
- #define GUARD_median_h
- // median.h
- #include <vector>
- double median(std::vector<double>);
- #endif
Expand|Select|Wrap|Line Numbers
- #ifndef GUARD_Student_info
- #define GUARD_Student_info
- // Student_info.h header file
- #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
// median.cpp
Expand|Select|Wrap|Line Numbers
- // source file for the median function
- #include <stdexcept> // to get the declaration of domain_error
- #include <vector> // to get the declaration of vector
- #include <algorithm> // to get the declaration of sort
- using std::domain_error; using std::vector; using std::sort;
- // compute the median of a vector<double>
- double median(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];
- }
Expand|Select|Wrap|Line Numbers
- #include <stdexcept>
- #include <vector>
- #include "grade.h"
- #include "median.h"
- #include "Student_info.h"
- using std::domain_error; using std::vector;
- // source file for the grade function
- double grade(double midterm, double final, double homework)
- {
- return 0.2 * midterm + 0.4 * final + 0.4 * homework;
- }
- // compute a students grade from midterm and final grades
- // and vectorof homework grades
- // this function does not copy its argument, becuase median does so for us
- 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);
- }
Expand|Select|Wrap|Line Numbers
- //source file for Student_info related functions
- #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)
- {
- // read and store the students name, midterm and final exam grades
- is >> s.name >> s.midterm >> s.final;
- read_hw(is, s.homework); // read and store all the students homework grades
- return is;
- }
- // read homewrok grades from an input stream into a vector<double>
- istream& read_hw(istream& in, vector<double>& hw)
- {
- if (in) {
- // get rid of prvious contents
- hw.clear();
- // read homewrok grades
- double x;
- hw.push_back(x);
- // clear the stream so that the input will work for the next student
- in.clear();
- }
- return in;
- }