Deleting pointers

My teacher was saying I didn't have to delete my pointers in this case since I was in the main. But I'm trying to understand why my pointers wouldn't delete regardless. The code is failing where I have it in bold. I get a Debug Assertion Failed error.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

struct Student
{
	string name;
	int ncourses;								
	string *courses;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
int main()
{
	ifstream fin;
	int sizeA;

	fin.open("courses.txt");
	if (fin.fail())
	{
		cerr << "Open Failure\n\n";
		system("pause");
		exit(1);
	}

	fin >> sizeA;
	fin.ignore();
	Student *list = new Student[sizeA];

	for (int ct = 0; ct < sizeA; ct++)
	{
		getline(fin, list[ct].name);
		fin >> list[ct].ncourses;
		list[ct].courses = new string[list[ct].ncourses];
		for (int k = 0; k < list[ct].ncourses; k++)
			fin >> list[ct].courses[k];
		fin.ignore();
	}

	fin.close();

	cout << "Enter menu choice or Q to quit:\n";
	cout << "D to display all students and courses\n";
	cout << "S to display courses for a student\n";
	cout << "C to diplay students taking a course\n\n\n";

	menu(list, sizeA);

	for (int i = 0; i < sizeA; ++i)
		delete list[i].courses;
	delete list;

	cout << endl;
	return 0 << system("pause");
}

Last edited on
You are using delete instead of delete[].

If you use new, you use delete.

If you use new[], you use delete[].
Thanks a bunch I didn't even notice what I did there.
Topic archived. No new replies allowed.