I am getting the error Segmentation fault (core dumped) when I run my code. It compiles and I can enter the information but when it displays the info, it doesn't display "Employees w/ no bonus , avg bonus, high bonus" So I'm guessing something is wrong there but I'm not sure what it is. Any help is greatly appreciated. Thank you
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;
}
}
// 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
}
}
This is illegal in C++. NUM_HOUR must be a compile time constant. (And generally speaking identifier tokens that are all-caps are reserved for macros or constants.)
Do you pay attention to compiler warnings? You have several lines of unreachable code that your compiler is likely to warn you about.
In this 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 access outside the bounds of the emp and employee arrays.
I changed NUM_HOUR to a const int NUM_HOUR = 0; and I did the same for NUM_SALARY
I can't say that's much of an improvement. Zero-sized arrays of automatic storage duration are not legal either (and if they were, using that array to store anything would be disastrous.)
Your line numbers and that error message mean nothing to me since they don't match up with the code you've supplied.
Can you explain a bit more on how I'm accessing outside the bounds for emp and employee?
If you had one salaried employee and one hourly employee, then numInfo would be 2, but emp and employee would have only one element each, so you would be accessing outside the bounds of each array when i was 1.