Error code

Hi all, I really need some help with a program I am creating. I keep getting segmentation fault (core dumped) when my program tries to display some information. I am creating a program that gathers employees info an It calculates their total pay. I have most of the program but I have a feature that displays salary workers and their bonus category. (no bonus, avg bonus, high bonus) It is not wanting to work. It will show list of employees but underneath that it says segmentation fault (core dumped) I have read my book and looked through google, and looked through my code but I don't know what it is. If anyone could help me figure it out, I'd really appreciate it. Thank you. I will post my code in different parts because it doesn't all fit here.

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
  #include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <cctype>
#include <sstream>
#include <set>
#include <iomanip> 

using namespace std;


//Create enumeration that contains bonus information 
enum BonusAvailability{NO_BONUS, AVG_BONUS, HIGH_BONUS};


//Create a struct that contains the employees personal information
struct PersonalInfo
{
	string fName,
		   lName,
		   title;
};

//Create a struct for hourly workers, calls personal info struct 
struct HourlyW
{
	PersonalInfo pData ; 
	double hoursWorked; 
	double hourlyRate;
};

//Create a struct for salary workers, calls personal info struct
struct SalaryW
{
	PersonalInfo pData;
	double salary;
	double bonus;
	BonusAvailability bData;
	
};
Last edited on
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
int main()
{
	
	
	int numInfo; // Holds number of employess to be entered 
	int NUM_HOUR = 0; // Holds number of hourly workers 
	int NUM_SALARY = 0; // Holds number of salary workers
	char selection; // If user wants to continue 
	char selection2; // If user wants to continue 
	double tHour; // Total hourly 
	double tSalary; // Total salary
	BonusAvailability b; // Create b under Bonus Availability 
	
				  // Get number of employees to be entered 
				  cout << "Enter the number of employees you will be entering information for: ";
				  cin >> numInfo;
				  
				  
				  
				  // If number is negative, show error message  
				  while (numInfo < 0)
				   {
					   cout << "Invalid number or negative number! Try again!" << endl;
					   exit(1);
				   }
				  // Get number of hourly workers 	  
				  cout << "How many are hourly workers?" << endl;
				  cin >> NUM_HOUR;
				  HourlyW employee[NUM_HOUR]; // Create employee under Hourly W with size being hourly workers
				 
				  
				  // If number is negative, show error message
				  while (NUM_HOUR < 0)
				   {
					   cout << "Invalid number or negative number! Try again!" << endl;
					   exit(1);
				   }
				  
				  // Get number of salary workers 
				  cout << "How many are salary workers?" << endl;
				  cin >> NUM_SALARY;
				  SalaryW emp[NUM_SALARY]; // Create emp under Salary W with size being salary workers
				  
				  
				  // If number is negative, show error message 
				  while (NUM_SALARY < 0)
				   {
					   cout << "Invalid number or negative number! Try again! " << endl;
					   exit(1);
				   }
				   
				   // If sum of hourly and salary is less than or greater than the total number of employees entered show error message 
				   while ((NUM_HOUR + NUM_SALARY) > numInfo || ( NUM_HOUR + NUM_SALARY < numInfo))
				   {
					   cout << "Hourly workers plus Salary workers does not equal " << numInfo << endl;
					   cout << "Try again " << endl;
					   cout << "" << endl;
					   cout << "How many hourly workers?" << endl;
					   cin >>NUM_HOUR;
					   while (NUM_HOUR < 0)
						{
							cout << "Invalid number or negative number!" << endl;
							exit(1);
						}
						cout << "How many are salary workers?" << endl;
						cin >> NUM_SALARY;
						while (NUM_SALARY < 0)
							{
								cout << "Invalid number or negative number!" << endl;
								exit(1);
							}
						if (NUM_HOUR + NUM_SALARY == numInfo)
						{
							break;
						}
				   }
				 
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// If hourly workers is greater than 0, run this to get the information
				  if (NUM_HOUR > 0)
				  {
					cout << "Hourly Workers!" << endl;
					cout << "---------------" << endl;
				  
					for (int i = 0; i < NUM_HOUR; i++)
					{
						  
					  
					  cout << "Enter first name: ";
					  cin >> employee[i].pData.fName;
					  cout << "" << endl;
					  cout << "Enter last name: ";
					  cin >> employee[i].pData.lName;
					  cout << "" << endl;
					  cout << "Enter their title: ";
					  cin >> employee[i].pData.title;
					  cout << "" << endl;
					  cout << "Enter the hours worked: ";
					  cin >> employee[i].hoursWorked; 
					  
					  // If hours worked is less than 0 or greater than 80, show error message 
					  while (employee[i].hoursWorked < 0 || employee[i].hoursWorked > 80)
					  {
						  cout << "Incorrect input!" << endl;
						  cout << "" << endl;
						  cout << "Enter the hours worked: ";
						  cin >> employee[i].hoursWorked;
						  if (employee[i].hoursWorked > 0 || employee[i].hoursWorked < 80)
							  break;
					  }
					  cout << "" << endl;
					  cout << "Enter the hourly rate: ";
					  cin >> employee[i].hourlyRate;
					  cout << "" << endl;
					  //tHour = employee[i].hoursWorked * employee[i].hourlyRate; // Get total hourly pay 
					  
					  //Ask if they want to continue 
					  cout << "Would you like to continue? Y or N" << endl;
					  cin >> selection;
					  if (selection == 'y' || selection == 'Y')
					  {
						  continue;
					  }
					  else 
					  {
						  break;
					  }
					 // cout << "Total " << tHour << endl;  //TEST 
					  
				    
				    } 
			      }
				  
				  
				 // If number of salary workers is greater than 0, run this to get the information  
				 if (NUM_SALARY > 0)
				 {
					cout << "Salary Workers!" << endl;
					cout << "---------------" << endl;
					for (int i = 0; i < NUM_SALARY; i++)
					{
						
						cout << "Enter first name: ";
						cin >> emp[i].pData.fName;
						cout << "" << endl;
						cout << "Enter last name: ";
						cin >> emp[i].pData.lName;
						cout << "" << endl;
						cout << "Enter their title: ";
					    cin >> emp[i].pData.title;
					    cout << "" << endl;
						cout << "Enter salary: ";
						cin >> emp[i].salary;
						
						// If salary is less that 0, show error message 
						while (emp[i].salary < 0)
						{
						  cout << "Incorrect input!" << endl;
						  cout << "" << endl;
						  cout << "Enter the salary: ";
						  cin >> emp[i].salary;
						  if (emp[i].salary > 0)
							  break;
						}
					  
						cout <<"" << endl;
						cout << "Enter bonus: ";
						cin >> emp[i].bonus;
						
						// If bonus is less than 0, show error message 
						while (emp[i].bonus < 0)
					    {
						  cout << "Incorrect input!" << endl;
						  cout << "" << endl;
						  cout << "Enter bonus: ";
						  cin >> emp[i].bonus;
						  if (emp[i].bonus > 0)
						  {
							break;
						  }
						}
						
						
						tSalary = emp[i].salary + emp[i].bonus; // Hold total pay for salary workers 
						//cout << "Salary is " << tSalary << endl; //TEST 
						
						cout << "" << endl;
						
						//See if user wants to continue 
					    cout << "Would you like to continue? Y or N" << endl;
					    cin >> selection2;
					    if (selection2 == 'y' || selection2 == 'Y')
							{
								continue;
							}
						else 
							{
								break;
							}
					  
					  // See what category the salary workers fall under 
					  if (emp[i].bonus == 0)
					  {
						  b = static_cast<BonusAvailability> (0);
						 
					  }
					  else if  (emp[i].bonus > 0 && emp[i].bonus <= 5000)
					  {
						  b = static_cast<BonusAvailability> (1);
					  }
					  else if (emp[i].bonus > 5000)
					  {
						  b = static_cast<BonusAvailability>(2);
					  }
						cout << "" << endl;
						//cout << "Bonus is " << b << endl; //TEST 
					}
					
				 }
				 
				 //Begin displaying information 
				 cout << "      List of employees              " << endl;
				 cout << "    --------------------         " << endl;
				 cout <<    " First  " << "  Last  " << "     Total Pay " << endl;
				 cout << " -------------------------------" << endl;
				 
														
					for (int i = 0; i < numInfo; i++)
					{
							if (NUM_HOUR > 0)
							cout << left << setw(10) <<  employee[i].pData.fName << setw(12) << employee[i].pData.lName << setw(10) << employee[i].hoursWorked * employee[i].hourlyRate << endl;
					
							cout << " " << endl;
						if (NUM_SALARY > 0)
						{
							cout << left << setw(10)<< emp[i].pData.fName  << setw(12) << emp[i].pData.lName << setw(10) <<  emp[i].salary + emp[i].bonus << endl;

						}
					}
					cout << "" << endl;
					
					//Show bonus for salary workers 
					
						cout << "  Employees with NO bonus" << endl;
						cout << "---------------------------" << endl;
						cout << " First  " << " Last  " << endl; 
						cout << "-------------------------------";
					
						for (int i = 0; i < numInfo; i++)
						{
						 
						  if (emp[i].bonus == 0)
						  {
							  cout << left << setw(10) << emp[i].pData.fName << setw(12) << emp[i].pData.lName << endl;
						  }
						}
						
						cout << "" << endl;
						cout << "  Employees with AVG bonus" << endl;
						cout << "---------------------------" << endl;
						cout << " First  " << " Last  " << endl; 
						cout << "-------------------------------"; 
						
						 for (int i = 0; i <  numInfo; i++)
						 {
							if (emp[i].bonus == 1)
						  {
							  cout << left << setw(10) << emp[i].pData.fName << setw(12) << emp[i].pData.lName << endl;
						  }
						 }
						 cout << "" << endl;
						cout << "  Employees with HIGH bonus" << endl;
						cout << "---------------------------" << endl;
						cout << " First  " << " Last  " << endl; 
						cout << "-------------------------------";
						
						 for (int i = 0; i < numInfo; i++)
						 {
							if(emp[i].bonus == 2)
						  {
							  cout << left << setw(10) << emp[i].pData.fName << setw(12) << emp[i].pData.lName << endl;
						  }
						 }
						 
						
			 
	return 0;
}


			

Last edited on
first of all, segfault always appear when you access a byte or block of memory you dont own.

look at your code
1
2
3
4
5
6
7
8
9
10
11
12
for (int i = 0; i < numInfo; i++)
					{
							if (NUM_HOUR > 0)
							cout << left << setw(10) <<  employee[i].pData.fName << setw(12) << employee[i].pData.lName << setw(10) << employee[i].hoursWorked * employee[i].hourlyRate << endl;
					
							cout << " " << endl;
						if (NUM_SALARY > 0)
						{
							cout << left << setw(10)<< emp[i].pData.fName  << setw(12) << emp[i].pData.lName << setw(10) <<  emp[i].salary + emp[i].bonus << endl;

						}
					}

you overdo a single loop to display employee and emp which their index's sum is equivalent to numInfo. does that makes sense?

lets say i entered 10 as the number of workers,


for (int i = 0; i < numInfo; i++)
this loop will be the same as
for (int i = 0; i < 10; i++)

next if i entered 5 for hourly workers
and 5 for salary workers the value of
 
NUM_HOUR
will be 5 and NUM_SALARY will be 5
and employee array size will be 5 and same with emp array

since you didnt have any break statement in loop or condition that if i become 4 the array will not be accessible anymore, the for loop will be force to finish until the i becomes 9 and access the array every iteration of the loop







Last edited on
Topic archived. No new replies allowed.