UNABLE TO CALL DECONSTRUCTOR

Hi guys, I need help. The only problem with this code is it won't call my destructor. Destructor is suppose to run automatically even though you don't call it. However, I don't know why the destructor would run.

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class cars{

	public:
	cars (unsigned sizearray);
	string searchCarname () const;
	~cars ();//destructor to de-allocate borrowed memory

	private:

	unsigned newsize;
	string * car_no;
};

int main ()
{
unsigned sizearray;
string name;

cout << "Enter the size of the array: " << endl;
cin >> sizearray;

cars a(sizearray);

cout << " " << a.searchCarname() << endl;

system("pause");

}

cars::cars (unsigned sizearray): newsize (sizearray) //CONSTRUCTOR
{
	int count;
	string newcarname;
	
	cout << "Newsize: " << newsize << endl;

	car_no = new string [newsize];

	for (count = 0; count < newsize; count++)
	{
	cout << count << " Enter car name: ";
	cin >> newcarname;

	car_no[count] = newcarname;
	
	}

	for (count = 0; count < newsize; count++)
	{
	cout << car_no[count] << endl;
	
	}

	

}

string cars::searchCarname() const
{
	int count;
	int switcher = 1;
	string searchcarname = "default";

	cout << "Enter the name of the car you want to search: ";
	cin >>  searchcarname;


	for (count = 0; count < newsize; count++)
	{

	if (car_no[count] == searchcarname)
	{
	cout << "CAR FOUND in parking lot number: " << count;
	searchcarname = car_no[count];
	switcher = 0;
	}

	}

	if (switcher == 1)
	{
	searchcarname = "NO CAR FOUND";
	}

	return searchcarname;
}

cars::~cars() //destructor to de-allocate borrowed memory
{
cout << "Deleting memory" << endl;
delete [] car_no;
}
The destructor is called just fine:
Enter the size of the array: 
3
Newsize: 3
0 Enter car name: A
1 Enter car name: B
2 Enter car name: C
A
B
C
Enter the name of the car you want to search: A
CAR FOUND in parking lot number: 0 A
sh: pause: not found
Deleting memory

Process returned 0 (0x0)   execution time : 6.353 s
Press ENTER to continue.
Ohh I found it Athar! Well the "system ("pause");" was messing it up for me. I deleted the "system ("pause");" and ran without debugging. It ran like a champ.

thank you for your time
Topic archived. No new replies allowed.