2013-07-31 65 views
0

Accelerated C++的練習4-6要我編寫一個讀入並計算學生總體成績的程序。我有以下代碼:libC++ abi.dylib:終止調用拋出異常

// source file for 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 and store the student's name and midterm and final exam grades 
    is >> s.name >> midterm >> final; 

    std::vector<double> homework; 
    read_hw(is, homework); // read and store all the 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 from an input stream into a 'vector<double>' 
istream& read_hw(istream& in, vector<double>& hw) 
{ 
    cout << "I get into read_hw!" << endl; 

    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 input will work for the next student 
     in.clear(); 
    } 
    cout << "I get (just before) out of read_hw!" << endl; 
    return in; 
} 

通過以上的被稱爲主()這樣:

int main() 
{ 
    vector<Student_info> students; 
    Student_info record; 
    string::size_type maxlen = 0;  // the length of the longest name 

    // read and store all the students data. 
    // Invariant: students contains all the student records read so far 
    //   maxlen contains the length of the longest name in students 
    while (read(cin, record)) { 
     cout << "Get into read() while loop" << endl; 
     // find length of longest name 
     maxlen = max(maxlen, record.name.size()); 
     students.push_back(record); 
     cout << "Get to end of read() while loop" << endl; 
    } 
    cout << "I get out of read ok!!" << endl; // WE NEVER GET THIS FAR 

這裏是輸出:

(Canopy 32bit) joes-imac:4-6 david$ make test 
./main <data/gradesTest 
I get into read_hw! 
I get (just before) out of read_hw! 
name is: Moo 
grade is: 100 

Get into read() while loop 
Get to end of read() while loop 
I get into read_hw! 
I get (just before) out of read_hw! 
name is: Moore 
grade is: 79.4 

Get into read() while loop 
Get to end of read() while loop 
I get into read_hw! 
I get (just before) out of read_hw! 
name is: Norman 
grade is: 72.8 

Get into read() while loop 
Get to end of read() while loop 
I get into read_hw! 
I get (just before) out of read_hw! 
libc++abi.dylib: terminate called throwing an exception 
/bin/sh: line 1: 20610 Abort trap: 6   ./main < data/gradesTest 
make: *** [test] Error 134 
(Canopy 32bit) joes-imac:4-6 david$ 

爲什麼會失敗?爲什麼我們似乎在程序失敗(數據輸入末尾沒有空白行或類似的東西)時做空白運行(即沒有讀取任何數據)?

+0

年級功能在哪裏? – doctorlove

+0

我的猜測是你在某處濫用內存,並且有東西被損壞。您需要確定它崩潰的位置(使用調試器或Valgrind之類的東西),然後您需要向我們提供所有相關的代碼。 –

回答

2

你最後一個循環的輸入是什麼?您是否試圖通過向midtermfinal值發送非數字值來結束循環?它看起來像是在使用這些數字計算分數時發生錯誤。我的猜測是嘗試這樣做:

if (is) 
{ 
    s.grade = grade(midterm, final, homework); 
} 
+0

是的,上面的作品...在我的輸入上,我用EOF結束數據(至少這是我認爲我在做的!) – mbbxedh2

相關問題