结构的分段错误错误

时间:2017-07-30 10:55:50

标签: c++ structure

我不知道分割错误来自哪里......有什么想法吗? 我正在使用结构进行作业

      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;
}

我似乎无法找到分段错误的来源。甚至从头开始重新启动整个任务,但似乎仍然会出错。

有人可以帮助或指出故障的来源。

在这里,我们有上面代码中包含的structs.h文件

#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 个答案:

答案 0 :(得分:1)

在那里有很多逻辑错误,根本原因(或原因;有相当多的候选人)几乎可以在任何地方。

getStudentIndex返回-1,除非学生是数组中的最后一个,并且是您添加的第一个学生的不确定值,因此将第一个学生添加到主题中是未定义的。

addStudent只有在学生已经参加该主题时才会添加。

它也(由于一些莫名其妙的原因)分配了两个Students的数组,使第一个元素未初始化。
当然,使用第一个元素是未定义的。

在另一个分支中,它首先声明分配的学生数量为* 1.5,但只分配+ 1
这无疑会导致问题。

addTestResult中的递归永远不会终止。

最有可能出现其他问题 - 这只是一瞥 从修复这些开始。

了解构造函数和析构函数,这样你就可以摆脱那些&#34;工厂&#34;和&#34;销毁&#34;功能

相关问题