whats wrong with my distructor??

can someone plz tell me whats wrong with my destructor?
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
#include <iostream>
#include <string>

using namespace std;
enum bank {HBSC,Citigroup,Barclays};

class employee
{
	int* id;
	char* name;
	int daysNotPaid;
	bank mybank;
	int accountnum;
	int date[8];
	double balance;
public:
	employee();
	employee(int idnum[9] ,const char*);
	int getid();
	char getname();
	bank getbank();
	int getbankaccount();
	double egtbalance();
	void setdays(int);
	void setbank(bank);
	void setaccountnum(int);
	void setdate(int);
	double setbalance(double);
	void work();
	void printpersoninfo();
	~employee();
};


employee::employee()
{
	id=new int[9];
};

employee::employee(int idnum[9],const char* ename)
{
	id=new int [9];
    for(int i=0;i<9;i++)
		id[i]=idnum[i];
	strcpy_s(name,30,ename);
}

employee::~employee()
{
	cout<<"destructor as been activated"<<endl;
	delete [] id;
};

int main()
{
	int arr[9];
	cin>>arr[9];
	employee emp(arr,"Al");

	return 0;
	
}
What makes you think something is wrong with it?
you have an error here:
1
2
3
        int arr[9];
	cin>>arr[9];
	employee emp(arr,"Al");

you are using an array which is not initialized
cin>>arr[9];
only initialize the 10th element which is not in the array and this causes problems with the destructor because I don't know why the delete [] knows somehow that you used it and crashes
you should initialize it like that
1
2
for(int i=0;i<9;i++)
       cin>>arr[i];
@Disch

i assumed somehitng's wrong with it because the program didnt print the alert i creatred in the destructor

@jewelcpp

but if i want the user to enter id (int array[9]) in one line how can i do it?

*thanks both for the hlp
I think this syntax will do the job you just separate the with spaces
try it
Last edited on
Your private member name isn't initialized, so the call

strcpy_s(name,30,ename);

normally results in an access violation.
print the alert


What alert?

It'd be much easier to help you if you tell us what's wrong.

EDIT:

Or maybe I'm just being bitter. Other people seem to be helping so maybe I should just shut up.

For your future reference, though.
Last edited on
WARNING

In your main function when using cin >> arr[9];, you should run into an error since the array only to goes to index number 8, since it starts indexing at 0 it only goes up to 8. 9 is not part of the array's index.

EDIT: @Disch In his/her destructor he/she as a cout statement. That's the alert.
Last edited on
Oh. =x. I misread what he typed. Now I feel like an ass. Haw.

Sorry.
Last edited on
Topic archived. No new replies allowed.