2017-07-30 121 views
-3

我不知道分段錯誤來自哪裏......任何想法? 我與結構的工作分配結構中的分段錯誤錯誤

 TestResult testResultFactory(std::string name, double mark) 
     { 
       //creating an object of TestResult 
       TestResult car; 
       car.name = name; 
       car.mark = mark; 

       return car; 
     } 


    Student studentFactrory(std::string name) 
    { 
      //Creating an object of student 
      Student house; 
      house.name = name; 
      house.testResults = 0; 
      house.numTestResults = 0; 

      return house; 
    } 


void addTestResult(Student * student, std::string testName, double testMark) 
{ 
    //First we need to create a new array 
    (student->numTestResults)+=1; 
    TestResult *newTestArray = new TestResult[(student->numTestResults)]; 

    //Now we loop through the old array and add it to the new one 
     int index = (student->numTestResults); 

      for (size_t i = 0; i < (index-1); i++) 
      { 
       newTestArray[i] = testResultFactory((student->testResults[i].name),(student->testResults[i].mark)); 
      } 

     //Now we need to add the new student to the end of the array 
     newTestArray[index] = testResultFactory(testName, testMark); 

     (student->testResults) = newTestArray; 
} 

string studentBest(Student const * student) 
{ 
    //create variables as temps 
    string highestName; 
    double highestMark; 
    int index = (student->numTestResults); 

    //Setting the two variables to the first value 
    highestName = (student->testResults[0].name); 
    highestMark = (student->testResults[0].mark); 

    //Using a while loop to compare and get the best 
     for (size_t i = 0; i < index; i++) 
     { 
      if((student->testResults[i].mark)> highestMark) 
      { 
      highestMark = (student->testResults[i].mark); 
      highestName = (student->testResults[i].name); 
      } 
     } 

    //returning the string they want 
    string send = (highestName)+ " "+ doubleToString(highestMark)+ "%"; 

    return send; 
    } 


double studentAverage(Student const * student) 
{ 
    //Variables used as temps 
    double average = 0; 
    double counter = 0.0; 
    double running = 0; 
    int index = (student->numTestResults); 

    //Now we need to loop through each one and add to running and counter 
     for (size_t i = 0; i < index; i++) 
     { 
      counter++; 
      running += (student->testResults[i].mark); 
     } 

    //calculating the average; 
    average = (running)/counter; 

    return average; 
} 


void destroyStudent(Student * student) 
{ 
    delete [] (student->testResults); 
    (student->testResults)=0; 
} 


Subject subjectFactory(std::string name) 
{ 
    //Creating an object to use in subject factory 
    Subject lamp; 

    lamp.name = name; 
    lamp.numStudents = 0; 
    lamp.studentsAllocated = 0; 
    lamp.students = 0; 

    return lamp; 
} 

我的猜測是因爲出界陣列或不工作正常的指針發生了錯誤。

int getStudentIndex(Subject const * subject, std::string studentName) 
{ 
    int index; 
    int count = (subject->numStudents); 

    //loop to find the names and set index 
    for (size_t i = 0; i < count; i++) 
    { 
     if(studentName == ((subject->students[i].name))) 
     { 
      index = i; 
     } 

     else index = -1; 
    } 

    return index; 
} 


void addStudent(Subject * subject, std::string studentName) 
{ 
    //Variables as temps 

    Student *pointer =0; 
    int index = getStudentIndex(subject,studentName); 

    if(index != -1) 
    { 
    //Now we need to see if they are large enough 

    if((subject->studentsAllocated)==0) 
    { 
     //Set the allocated to 2 
     (subject->studentsAllocated) = 2; 
     pointer = new Student[2]; 

     //Figure this out later 
     pointer[1] = studentFactrory(studentName); 

     (subject->students) = pointer; 
    } 

    else 
     { 
     //increase SA with 1.5 
      (subject->studentsAllocated) = (subject->studentsAllocated) * 1.5; 
      pointer = new Student[(subject->studentsAllocated)+1]; 
      int count = (subject->studentsAllocated); 
      //Now we need to put all the other students in 
      for (size_t i = 0; i < count-1; i++) 
      { 
       pointer[i] = (subject->students[i]); 
      } 

      pointer[(subject->studentsAllocated)+1] = studentFactrory(studentName); 
      (subject->studentsAllocated) += 1 ; 
     } 

     //Once done just seet one equal to 
     (subject->students) = pointer; 
    } 
    else return; 

} 

void removeStudent(Subject * subject, std::string studentName) 
{ 
    //First get temps 
    int index = getStudentIndex(subject ,studentName); 
    int number = (subject->studentsAllocated); 
    int i = index; 

    //delete student 
    if(index == -1) return; 

    destroyStudent(&(subject->students)[index]); 

    //loop to shift the things 
    while (i<(number -1)) 
    { 
     (subject->students)[i] = (subject-> students[i+1]); 
    } 

    //Removing the last one 
    (subject->numStudents) -= 1; 
} 

bool addTestResult(Subject * subject, std::string studentName, std::string testName, double testMark) 
{ 
    int index = getStudentIndex(subject ,studentName); 

    if(index != -1) 
    { 
     addTestResult(&(subject->students [index]),testName,testMark); 
     return true; 
    } 
    else 
     return false; 
} 

void printSubjectSummary(Subject const * subject) 
{ 
    cout<<(subject->name)<< ": with "<<(subject->numStudents)<<" students"<<endl; 
    //Variables to use in the loop 
    size_t indexLoop = subject->numStudents; 
    int i=0; 

    while (i< indexLoop) 
    { 
     cout<<(subject->students[i].name)<<" Average: "<<studentAverage(&(subject->students[i]))<<", Best: "<<studentBest(&(subject->students[i]))<<endl; 
    } 
} 
void destroySubject(Subject * subject) 
{ 
    //Variables 
    size_t indexLoop = subject->numStudents; 
    for (size_t i = 0; i < indexLoop; i++) 
    { 
      destroyStudent(&(subject->students[i])); 
    } 
    delete [] subject->students; 
    subject->students =0; 
} 

我似乎無法找到分割錯誤來自哪裏。即使從頭開始重新開始整個任務,似乎仍然會出錯。

有人可以幫助或指出故障可能來自何處。

在這裏,我們有有在有這麼多的邏輯錯誤的根本原因是包含在我上面的代碼

#ifndef STRUCTS_H 
#define STRUCTS_H 

struct TestResult{ 
    double mark;//the test mark as a percentage 
    std::string name;//the test name 
}; 
struct Student{ 
    std::string name; 
    TestResult * testResults;//an arry of TestResults 
    size_t numTestResults;//the number of results for this student (also the size of the array) 
}; 
struct Subject{ 
    std::string name; 
    Student * students;//an array of Students 
    size_t numStudents;//the number of students added to the subject 
    size_t studentsAllocated;//the size of the Student arry(must never be smaller that numStudents) 
}; 

#endif 
+1

請提供[mcve] –

+0

https://stackoverflow.com/help/mcve –

+1

實際崩潰說的是什麼?你可以發佈堆棧跟蹤嗎? – Shark

回答

1

的structs.h文件(或原因;也有相當多的考生)幾乎可以在任何地方。

getStudentIndex返回-1,除非學生是數組中的最後一個,並且爲第一個添加了不確定的值,因此將第一個學生添加到主題是未定義的。

addStudent只有在學生已經接受該科目的情況下才會添加學生。

它也(由於某些莫名其妙的原因)分配兩個Students的數組,使第一個元素未初始化。
使用這第一個元素當然是未定義的。

在另一個分支中,它首先聲稱分配的學生數量爲* 1.5,但後來只分配+ 1
這無疑會導致問題。

addTestResult中有一個遞歸永不終止。

最有可能的其他問題 - 這只是一個快速瀏覽。
從修復這些開始。

並瞭解構造函數和析構函數,以便可以擺脫那些「工廠」和「銷燬」函數。