c++ - libc++abi.dylib: terminate called throwing an exception -
exercise 4-6 of accelerated c++ wants me write program reads in , calculates student's overall grade. have following code:
// source file student_info-related functions #include "student_info.h" #include "grade.h" using std::istream; using std::vector; using namespace std; bool compare(const student_info& x, const student_info& y) { return x.name < y.name; } istream& read(istream& is, student_info& s) //void read(istream& is, student_info& s) { double midterm, final; // read , store student's name , midterm , final exam grades >> s.name >> midterm >> final; std::vector<double> homework; read_hw(is, homework); // read , store student's homework grades s.grade = grade(midterm, final, homework); cout << "name is: " << s.name << endl; /// cout << "midterm is: " << midterm << endl; /// cout << "final is: " << final << endl; cout << "grade is: " << s.grade << endl; cout << "" << endl; return is; } // read homework grades input stream 'vector<double>' istream& read_hw(istream& in, vector<double>& hw) { cout << "i read_hw!" << endl; if (in) { // rid of previous contents hw.clear(); // read homework grades double x; while (in >> x) hw.push_back(x); // clear stream input work next student in.clear(); } cout << "i (just before) out of read_hw!" << endl; return in; }
with above being called main() such:
int main() { vector<student_info> students; student_info record; string::size_type maxlen = 0; // length of longest name // read , store students data. // invariant: students contains student records read far // maxlen contains length of longest name in students while (read(cin, record)) { cout << "get read() while loop" << endl; // find length of longest name maxlen = max(maxlen, record.name.size()); students.push_back(record); cout << "get end of read() while loop" << endl; } cout << "i out of read ok!!" << endl; // never far
here output:
(canopy 32bit) joes-imac:4-6 david$ make test ./main <data/gradestest read_hw! (just before) out of read_hw! name is: moo grade is: 100 read() while loop end of read() while loop read_hw! (just before) out of read_hw! name is: moore grade is: 79.4 read() while loop end of read() while loop read_hw! (just before) out of read_hw! name is: norman grade is: 72.8 read() while loop end of read() while loop read_hw! (just before) out of read_hw! libc++abi.dylib: terminate called throwing exception /bin/sh: line 1: 20610 abort trap: 6 ./main < data/gradestest make: *** [test] error 134 (canopy 32bit) joes-imac:4-6 david$
why fail? why seem blank run (i.e. not read in data) program fails (the data input not have blank line @ end or that)?
what input last loop? trying end loop sending non-numeric values midterm
, final
values? looks error happening when calculating grade using numbers. guess try doing this:
if (is) { s.grade = grade(midterm, final, homework); }
Comments
Post a Comment