2012-02-23 41 views
0

我正在寫一個關於指針和內存管理的課程作業。在這個任務中爲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; 
} 

回答

2

你正在用operator new [](這是數組),但用operator delete而不是operator delete []釋放它。

你應該釋放它這樣的:

delete[] coursePTR; 

我也想不明白,爲什麼你使用sizePTR指針,而不是直接使用courses的。

順便說一句,這樣你就會知道,這不是一個'找到我的錯誤'網站。你有一個代碼審查網站。

+0

對不起,我不知道.. – sircrisp 2012-02-23 15:33:54

+2

沒問題。你活着,你學會:) – Asaf 2012-02-23 15:37:59

2

您需要更改

delete coursePTR; 

delete [] coursePTR; 

新總是需要與刪除一起使用,並且使用刪除[]的新的[],否則會發生不好的事情。