Deleting contents in my array.

Hi, I have this problem. I want to initialize too the contents of the array made by the user. When I use delete [] array; it errors. How can I delete the contents in my array? :( * btw I used structure in my datas.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

void irec(){
	char choice;
	cout << "WARNING: Do you want to clear all data? (Y/N) ---> ";
	cin >> choice;
	if (choice == 'Y' || choice == 'y')
	{	
		
		delete [] record;
		ofstream("c:delete.txt", ios_base::trunc);
		cout << "Data Deleted!";
		system("pause>0");
	}
	else
		cout << "**** Back to Menu ****";
		system("pause>0");
		
Last edited on
You've shown us nothing about what record is, and how it's defined. Nor have you told us anything about the error you're getting. How do you expect us to know why the delete statement isn't working, if you withhold information from us? Why would you even bother seeking help from us, if you're not prepared to actually tell us anything that would help us solve the problem?

My guess would be that you're trying to delete something you didn't allocate in the first place.

I'm sorry, lol. This is the error.

http://s58.photobucket.com/user/tntnsia/media/error_zpsb61bed31.jpg.html

and this is my whole code.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

struct Record {
	char studnum[50];
	char name[50];
	char course[50];
	float gpa;

};
void irec();
void addcontact(Record r);
void viewcontacts();
void newline();
Record record[100];
int index = 0;
int main(){

	int choice;
	while (1){
		system("cls");
		cout << "XYZ Computer College\n\n";
		cout << "What would you like to do?\n";
		cout << "[1]- Initialize Record\n";
		cout << "[2]- Add Record\n";
		cout << "[3]- Show Record\n";
		cout << "[4]- Exit\n";

		cout << "Please enter your choice: ";
		cin >> choice; 
		cout << "--------------------------------------------------\n\n";
		switch (choice){
		case 1:
			irec();
			break;
		case 2:
			Record r;
			char buff[2];
		
			cout << "Enter Contact Details\n";
			cout << "Student Number: ";
			cin.getline(buff, 2);
			cin.getline(r.studnum, 50);
			cout << "Name: ";
			cin.getline(r.name, 50);
			cout << "Course: ";
			cin.getline(r.course, 50);
			cout << "GPA: ";
			cin >> r.gpa;
			cout << r.gpa;
			addcontact(r);
			break;
		case 3:
			viewcontacts();
			break;
		case 4:
			exit(1);
			break;
		default:
			"Invalid Choice! :(";
			system("pause");

		}

	}

	return 0;
}

void addcontact(Record r){

	
		ofstream fout("c:\\records.txt", ios::out);
		record[index] = r;
		index++;
		fout << setw(10) << "Student No." << setw(15) << "Student Name" << setw(15) << "Course" << setw(20) << "GPA" << endl;
		for (int i = 0; i < index; i++){

			fout << setw(10) << record[i].studnum
				<< setw(15) << record[i].name
				<< setw(15) << record[i].course
				<< setw(20) << record[i].gpa << endl;
		}
		fout.close();
	
}

void newline()
{
	char s;
	do { cin.get(s); } while (s != '\n');
}

void viewcontacts() {

	cout << setw(10) << "Student No."
		<< setw(15) << "Name"
		<< setw(15) << "Course"
		<< setw(20) << "GPA\n";

	for (int i = 0; i < index; i++)
	{
		
		cout << setw(10) << record[i].studnum
			<< setw(15) << record[i].name
			<< setw(15) << record[i].course
			<< setw(20) << record[i].gpa << endl;
		
	}

	system("pause>0");
}

void irec(){
	char choice;
	cout << "WARNING: Do you want to clear all data? (Y/N) ---> ";
	cin >> choice;
	if (choice == 'Y' || choice == 'y')
	{	
		
		delete[] record;
		ofstream("c:\\records.txt", ios_base::trunc);
		cout << "Data Deleted!";
		system("pause>0");
	}
	else
		cout << "**** Back to Menu ****";
		system("pause>0");
		
}
MikeyBoy is correct. you arent dynamically allocating memory for your array so it makes no sense to attempt to deallocate.

when i compile your code i get this:
(123): warning C4154: deletion of an array expression; conversion to pointer supplied

which is an (admittedly obscure) indication of this.
Last edited on
Topic archived. No new replies allowed.