Segmentation error

Hi, I'm facing this problem in c++. I'm able to compile my main.cpp but when I run it, I can input data into console, but upon finishing the data inputs, There is a segmentation fault at the bottom and it never state which line or which file is causing it. How do I go about showing my problem faced here? Do I quote both .cpp files in here? I'm new to c++ so hope things can be in layman terms thks
You need to post your code.
A segmentation fault is a runtime error.
Which is why you are not getting a nice error message sending you to a line in your source files.

A segmentation fault is usually caused when you try to access memory you shouldn't.

Please quote both source files, putting them in [code]int x=5; // source code [/code] brackets.
Employee class

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <sstream>

using namespace std;

/*================================================= Employee Class ====================================================*/

class Employee
{
private:
int id;
string name;
string dob;
string address;
string phoneNo;
string email;
int deptNo;

public:
Employee();
Employee(int empId, string empName, string empDOB, string empAdd, string phone, string emailAdd, int deptNum);
void setFields(int empId, string empName, string empDOB, string empAdd, string phone, string emailAdd, int deptNum);

// Get&Set Function for id
int getId();
void setId(int empId);
// Get&Set Function for name
string getName();
void setName(string empName);
// Get&Set Function for dob
string getDOB();
void setDOB(string empDOB);
// Get&Set Function for address
string getAddress();
void setAddress(string empAdd);
// Get&Set  Function for phoneNo
string getPhone();
void setPhone(string phone);
// Get&Set  Function for email
string getEmail();
void setEmail(string emailAdd);
// Get&Set  Function for deptNo
int getDeptNo();
void setDeptNo(int deptNum);
//float calculatePay(
//float calculateTax
void displayData();
//getBasicPay

// End of Employee class declarations
}; 		


/*================================================ Functions Implementations ==================================================*/

//Employee default constructor
Employee::Employee()
{
 id = 0;
 name = "";
 dob = "";
 address = "";
 phoneNo = "";
 email = "";
 deptNo = 0;
}

//Employee 2nd constructor
Employee::Employee(int empId, string empName, string empDOB, string empAdd, string phone, string emailAdd, int deptNum)
{
	id = empId;
	name = empName;
	dob = empDOB;
	address = empAdd;
	phoneNo = phone;
	email = emailAdd;
	deptNo = deptNum;
}

// Get&Set Function for id
int Employee::getId()
{
   return Employee::id;
}
void Employee::setId(int empId)
{
   Employee::id = empId;
}
// Get&Set Function for name
string Employee::getName()
{
   return Employee::name;
}
void Employee::setName(string empName)
{
   Employee::name = empName;
}
// Get&Set Function for dob
string Employee::getDOB()
{
   return Employee::dob; 
}
void Employee::setDOB(string empDOB)
{
   Employee::dob = empDOB;
}
// Get&Set Function for adress
string Employee::getAddress()
{
   return Employee::address;
}
void Employee::setAddress(string empAdd)
{
   Employee::address = empAdd;
}
// Get&Set Function for phoneNo
string Employee::getPhone()
{
   return Employee::phoneNo;
}
void Employee::setPhone(string phone)
{
   Employee::phoneNo = phone;
}
// Get&Set Function for email
string Employee::getEmail()
{
   return Employee::email;
}
void Employee::setEmail(string emailAdd)
{
   Employee::email = emailAdd;
}
// Get&Set Function for deptNo
int Employee::getDeptNo()
{
   return Employee::deptNo;
}
void Employee::setDeptNo(int deptNum)
{
   Employee::deptNo = deptNum;
}


/*void Employee::setFields(int empId, string empName, string empDOB, string empAdd, string phone, string emailAdd, int deptNum)
{
	id = empId;
	name = empName;
	dob = empDOB;
	address = empAdd;
	phoneNo = phone;
	email = emailAdd;
	deptNo = deptNum;
}*/

 


main class
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
#include <iostream>
#include <sstream>
#include <string>	
#include "Employee.cpp"
//#include "OrdinaryEmployee.h"
//#include "SeniorEmployee.h"

using namespace std;


const static int MAXIMUM_DATA_SIZE = 100;

// To prevent invalid input type (int)
const static int getline_i()
{
	while(cin.fail()) {
		cin.clear();
	}
	string s;
	getline(cin, s);
	stringstream ss;
	ss.str(s);

	int i;
	ss >> i;
	return i;
	}
	
// To prevent invalid input type (float)
const static float getline_f()
{
	while(cin.fail()) {
		cin.clear();
	}
	string s;
	getline(cin, s);
	stringstream ss;
	ss.str(s);

	float f;
	ss >> f;
	return f;
}

/*void Employee::addEmpData((int empId, string empName, string empDOB, string empAdd, string phone, string emailAdd, int deptNum)
{
    cout<<"\n[  Add new employee data  ]\n"<<endl
    	<<"Enter new id : ";
    	empId = getline_i();
    cout<<"Enter new name : ";
        getline(cin, empName);
    cout<<"Enter your date of birth : ";
    	getline(cin, empDOB);
    cout<<"Enter your address : ";
    	getline(cin, empAdd);
    cout<<"Enter your phone number : ";
    	getline(cin, phone);
    cout<<"Enter your email address : ";
    	getline(cin, emailAdd);
    cout<<"Enter your dept. number : ";
    	deptNum = getline_i();
    cout<<"\n\nNew employee data have been keyed into the system...\n";
}*/

// Add new employee data function
Employee addEmpData()
{
int id;
string name;
string dob;
string address;
string phoneNo;
string email;
int deptNo;

cout<<"\n[  Add new employee data  ]\n"<<endl
<<"Enter new id : ";
id = getline_i();
cout<<"Enter new name : ";
getline(cin, name);
cout<<"Enter your date of birth : ";
getline(cin, dob);
cout<<"Enter your address : ";
getline(cin, address);
cout<<"Enter your phone number : ";
getline(cin, phoneNo);
cout<<"Enter your email address : ";
getline(cin, email);
cout<<"Enter your dept. number : ";
deptNo = getline_i();
cout<<"\n\nNew employee data have been keyed into the system...\n";
}

int main()
{
static int dataCounter = 0;
Employee empArray[MAXIMUM_DATA_SIZE];

int choice = 0;

while(true) {

	cout<<"\n\n";
	cout<<"Welcome to the Salary System"  <<endl
	    <<"1) Add New Employee Data"           <<endl
	    <<"2) Display Employee's Profile" <<endl
	    <<"3) Quit"			      
	    <<endl;
	// Waiting for user to enter choice
	cout<<"Please enter your choice: ";
	choice = getline_i();
	
	if( choice < 1 || choice > 3 ) {
	cout<<"Invalid input ! Please enter your choice again...";
	choice = 0;
	}
	
	if(choice == 1) {
	if(dataCounter < MAXIMUM_DATA_SIZE)
	{
	empArray[dataCounter++] = addEmpData();
	}
	    		} // End of option 1
	    
}
} 


Looks abit messy but hope u dun mind.. This assignment is to let us expose to inheritance,overriding and overloading.. Suppose to have 2 more subclass of Employee but i have not implemented them. Currently stuck here with the segmentation fault.

What I'm trying to do is to have this function to add new employee data and store them into an array.. have not figure out how to use vector so didn't touch on that.. But apparently my coding seems to have some big mistake somewhere or my logic is wrong..

Pls advice..

First, could you please explain to me why
1
2
const static int getline_i()
const static float getline_f()

and not
1
2
int getline_i()
float getline_f()
?

Second (and this is only a style thing): do not include cpp files. Change extension to hpp. This isn't too important though, but people are used to including header files, h, hpp, hxx whilst source files c, cpp, cxx are compiled.

When I tried to compile your code but I got linker errors because of the above.

As for your logic being wrong... nothing displays at choice 2 and program doesn't exit at choice 3.
Nevermind, I see you didn't complete that part yet.

Edit: no segmentation fault here though. Exactly what data do you input yourself?
Last edited on
I'm surprised what gcc will let you get away with.

All you functions that say they returns something should return something. addEmpData never attempts to fill in the Employee object that it promises to return, plus you never actually return one. I thought that was an error, but clearly gcc thinks it's ok--it just crashes the program at runtime instead.

Stop using static. You have no need for it here.

Don't include .cpp files, it breaks conventions.

Don't write code like this:
1
2
3
4
void Employee::setDeptNo(int deptNum)
{
   Employee::deptNo = deptNum;
}


do this instead:
1
2
3
4
void Employee::setDeptNo(int deptNum)
{
   deptNo = deptNum;
}
Actually i have no clue why to use static, my previous assignment had a req to state so like "float static ....."

Regards to the code, one of my friend suggest i do Employee:: so they know where to get the data from. I tried excluding that Employee:: before and it works too.

After u look at my code, which is the portion that have a prob?

Bout the include files.. If i do not include the cpp file, i thought my main.cpp will not link the other files together? Correct me if im wrong.. I thought in the main.cpp suppose to #include "Employee.cpp" so they are able the read the data in it.
$ warning: no return statement in function returning non-void [-Wreturn-type]


The linker links the files. You include header files, were the declarations are.
The include just do a copy-paste. So if you include a source files, is like having everything in just main.cpp
That increase build time (as you defeat the purpose of the linker), and you may have redefinition issues.
Also look for include guards.

g++ main.cpp -c #compiling
g++ employee.cpp -c

g++ main.o employe.o -o program.bin #linking
If you have an IDE, you need to create a project and add the sources to it.


Use a debugger and perform a backtrace.
Problem solved. I added return type for the function. thks!
Topic archived. No new replies allowed.