我正在为学习有关指针和内存管理的类分配程序。在此作业中,必须使用new运算符为学生和课程分配内存,该程序似乎可以正常运行,但是在打印完所有内容后,该程序崩溃了,并说它触发了断点。任何帮助,将不胜感激。

该错误位于此处,但我不确定这意味着什么
 /* verify block type */ _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct Student //Structs
    {
        string firstName, lastName, aNumber;
        double GPA;
    };
struct Course
    {
        int courseNumber, creditHours;
        string courseName;
        char grade;
    };

Student* readStudent();                                    // Function Prototypes
Course* readCourseArray(int&);
void computeGPA(Student *student, Course *courseArray, int coursesTaken);
void printSummary(Student *studentPTR, Course *courseArray, int courses);
int main()                                                //Main
{
    int courses = 0;
    Student *studentPTR = readStudent();
    Course *coursePTR = readCourseArray(courses);
    computeGPA(studentPTR, coursePTR, courses);
    printSummary(studentPTR, coursePTR, courses);


    delete studentPTR;
    delete coursePTR;
    system ("pause");
    return 0;
}

Student* readStudent()                                       // Read Student
{
    Student* student = new Student;
    cout<<"\nEnter students first name\n";
    cin>>student->firstName;
    cout<<"\nEnter students last name\n";
    cin>>student->lastName;
    cout<<"\nEnter students A-Number\n";
    cin>>student->aNumber;


    return student;
}

Course* readCourseArray(int &courses)                           //Read Courses
{
    cout<<"\nHow many courses is the student taking?\n";
    cin>>courses;
    const int *sizePTR = &courses;
    Course *coursePTR = new Course[*sizePTR];

    for(int count = 0; count < *sizePTR; count++)  //Enter course information
    {
        cout<<"\nEnter student "<<count+1<<"'s course name\n";
        cin>>coursePTR[count].courseName;
        cout<<"\nEnter student "<<count+1<<"'s course number\n";
        cin>>coursePTR[count].courseNumber;
        cout<<"\nEnter student "<<count+1<<"'s credit hours\n";
        cin>>coursePTR[count].creditHours;
        cout<<"\nEnter student "<<count+1<<"'s grade\n";
        cin>>coursePTR[count].grade;
    }


    return coursePTR;
}


void computeGPA(Student *studentPTR, Course *courseArray, int coursesTaken)             // Compute GPA
{
    double total = 0, hours = 0;
    for(int count = 0; count < coursesTaken; count++)
    {
        hours += courseArray[count].creditHours;
        if (courseArray[count].grade == 'A')
            total+=4;
        else if (courseArray[count].grade == 'B')
            total+=3;
        else if (courseArray[count].grade == 'C')
            total+=2;
        else if (courseArray[count].grade == 'D')
            total+=1;
        else if (courseArray[count].grade == 'F')
            total+=0;
    }
    studentPTR->GPA = (total / hours);
}


void printSummary(Student *studentPTR, Course *courseArray, int courses)
{
    cout<<"\nReport\n";
    cout<<"\nNumber   Name                    Hours  Letter Grade  Point Grade";
    cout<<"\n------   ----                    -----  ------------  -----------\n";
    for(int count = 0; count < courses; count++)
         cout<<setw(3)<<courseArray[count].courseNumber<<"   "<<courseArray[count].courseName<<setw(20)<<"   "<<courseArray[count].creditHours<<setw(5)<<"  "<<courseArray[count].grade<<endl;

    cout<<"\nStudent :"<<studentPTR->firstName<<" "<<studentPTR->lastName;
    cout<<"\nA-Number :"<<studentPTR->aNumber;
    cout<<"\nOverall GPA :"<<studentPTR->GPA;
}

最佳答案

您正在使用运算符new [](适用于数组)分配课程,但是要使用运算符delete而不是运算符delete []释放课程。

您应该这样释放它:

delete[] coursePTR;


我也不明白为什么您使用sizePTR指针而不是直接使用courses

顺便说一句,您一定会知道,这不是“发现我的错误”网站。您有一个代码检查站点。

10-07 19:12
查看更多