Need help in this 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
#include <iostream>
#include <string>
using namespace std;

class Employee{
public:
Employee();
void Set_Name();
string Get_Name();
void Set_ID();
int Get_ID();
void Set_Salary();
void Get_Salary();
~Employee();
int ID, salary,months;
int *p;
string name;
Employee(int ID, string name, int salary,int months);
	
};
Employee::Employee(){
	ID=0;
	salary=0;
	name="";
}
Employee::Employee(int _ID, string _name, int _salary,int _months){
	ID=_ID;
	name = _name;
	salary = _salary;
	_months=months;
	p=new int [months];
}
void Employee::Set_Salary(){
	cout << "Enter number of months: ";
	cin >> months;
	for( int i=0; i < months ; i++)
		cin >> p[i];
}
void Employee::Get_Salary(){
	for(int i=0;i<months;i++)
		cout << p[i] << endl;
}
void Employee::Set_ID(){
	cout << "Enter employee ID number: ";
	cin >> ID;
}
int Employee::Get_ID(){
	cout << ID;
	return ID;
}
void Employee::Set_Name(){
	cout << "Enter Employee Name: ";
	cin >> name;
}
string Employee::Get_Name(){
	cout << name;
	return name;
}
Employee::~Employee(){
	delete [] p;
}
int main(){
	Employee x;
	x.Set_ID();
	x.Set_Name();
	x.Set_Salary();
	x.Get_Salary();
	x.Get_ID();
	x.Get_Name();

	system("pause");
}

Everytime i try to run this code, i get an access violation error from the Set_Salary, i can enter in everything in the black box but when i get to the stage of Set_Salary, i get an access violation 0xC0000005, can any1 tell me where for it is not written anywhere :/
Last edited on
Hi, please edit your post and use code tags for all of your code- http://www.cplusplus.com/articles/jEywvCM9/

Your problem is that when you call Set_Salary, the array "p", which you should rename to something meaningful, does not exist. You create it in the constructor -

1
2
3
4
5
6
7
Employee::Employee(int _ID, string _name, int _salary, int _months){
	ID = _ID;
	name = _name;
	salary = _salary;
	_months = months;
	p = new int[months]; // here
}


meaning, when you then use it in Set_Salary - cin >> p[i]; it's not going to work since you never created it.
Hi, thanks for your reply, I changed the name and tagged the codes, and I renamed my pointer.
Anyways, I already declared the pointer in the class -
1
2
3
4
5
6
7
8
9
10
11
12
13
class Employee{
public:
Employee();
void Set_Name();
string Get_Name();
void Set_ID();
int Get_ID();
void Set_Salary();
void Get_Salary();
~Employee();
int *salary;
int ID,months;
string name;

Im not sure what you meant by saying to declare it in the constructor, how am I supposed to do that? Can you please be more precise
Yes. You created a pointer in your class. But that's it, it's a pointer.

What you are doing here -

1
2
for( int i=0; i < months ; i++)
		cin >> p[i];

is using the pointer as an array, which for obvious reasons wont work. Not before you do this -
p=new int [months]; which creates a dynamic array of size months, and now you can do cin >> pi[i];. Your problem is that you try and do that before you create the array.

The array is created on line 31 in the function constructor Employee::Employee(int _ID, string _name, int _salary,int _months){. You never call this
function
constructor and there fore the array is never created, that is why when you try to use the array in Set_Salary, it does not exist and doesn't work.

Hope this makes things more clear =)

Edit: Thanks @doug4 for the correction.
Last edited on
You have 2 constructors--1 default constructor (taking no arguments) and 1 constructor that takes 4 arguments. The 4-argument constructor creates the p array. The default constructor does not.

Unfortunately, line 63 shows you creating an Employee with a default constructor. The p array is not created, so you are not able to populate its elements. You need to do line 31 in your default constructor, too.

@TarikNeaj -- It's not a function at line 26, it's a constructor.
Topic archived. No new replies allowed.