Hey guys, I'm currently working on an assignment to create a C++ Employee class that can compute the net pay salary of hourly based employees and also find the average net pay for a small company. As far as I know, I've done everything that I am required, but when testing my main program I keep getting a segmentation fault and I don't know exactly what that means/what is causing the problem. Basically, where I am at right now is that when I attempt to print out the required information (employee names, net pay, gross pay etc.) the only thing that actually prints out are some numbers, but no names. I was wondering, could anyone take a look at my code and see if anything pops out to help me fix this problem?
#include "employee.h"
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <sstream>
Employee::Employee()
{
}
Employee::Employee(int id, std::string f_name, std::string l_name, double hoursworked, double hourlyrate)
{
this->tax = 30;
this->id = id;
this->f_name = f_name;
this->l_name = l_name;
this->hoursworked = hoursworked;
this->hourlyrate = hourlyrate;
}
int Employee::getID()
{
return id;
}
double Employee::getNetPay()
{
return getGrossPay() * (1 - (tax * 0.01));
}
double Employee::getGrossPay()
{
if(hoursworked > 40)
return (hoursworked * hourlyrate) * 1.5;
else
return (hoursworked * hourlyrate);
}
double Employee::getTax()
{
return tax;
}
double Employee::getHRate()
{
return hourlyrate;
}
double Employee::getHWorked()
{
return hoursworked;
}
std::string Employee::getFname()
{
return f_name;
}
std::string Employee::getLname()
{
return l_name;
}
void Employee::setFname(std::string f_name)
{
this->f_name = f_name;
}
void Employee::setLname(std::string l_name)
{
this->l_name = l_name;
}
void Employee::setHRate(double hourlyrate)
{
this->hourlyrate = hourlyrate;
}
void Employee::setHWorked(double hoursworked)
{
this->hoursworked = hoursworked;
}
std::ostream &operator << (std::ostream &obj, Employee p)
{
return obj << p.EString();
}
std::string Employee::EString()
{
std::string str = "";
{std::ostringstream oss;
oss << getID();
str+= oss.str();
}
str+="\t" + getFname();
str+="\t\t" + getLname();
{std::ostringstream oss;
oss << getHWorked();
str+="\t\t" + oss.str();
}
{std::ostringstream oss;
oss << getHRate();
str=+"\t$" + oss.str();
}
{std::ostringstream oss;
oss << getGrossPay();
str+="\t$" + oss.str();
}
{std::ostringstream oss;
oss << getTax();
str+="\t" + oss.str()+"%";
}
{std::ostringstream oss;
oss << getNetPay();
str+="\t$" + oss.str();
}
return str;
}
Here is my Main program
#include "employee.h"
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
string employeeInfo = "\t\t\tFordham University Payroll\nID\tFirst Name\tLast Name\tHours Worked\tHourly Rate\tGross Pay\tTax\tNet Pay\n\n";
srand ( time(NULL) );
string fName[5] = {"Richard" , "Peter" , "Margaret" , "Steven" , "Alex"};
string lName[5] = {"Brown" , "Dub" , "Laughington" , "Blutarsky" , "Mouse"};
cout << employeeInfo;
for(int i = 1; i <= 5; i++)
{
int hoursworked = rand() % 40 + 20;
int hourlyrate = rand() % 20 + 10;
int fRandom = rand() % 10;
int lRandom = rand() % 10;
Employee p(i, fName[fRandom], lName[lRandom], hoursworked, hourlyrate);
cout << p <<"\n";
}
cin.get();
return 0;
}
And lastly, my header file
#include <iostream>
#include <string>
class Employee {
private:
double hoursworked, hourlyrate, tax;
int id;
std::string f_name, l_name;
public:
Employee();
Employee(int id, std::string f_name, std::string l_name, double hoursworked, double hourlyrate);
int getID();
double getNetPay();
double getGrossPay();
double getTax();
double getHRate();
double getHWorked();
std::string getFname();
std::string getLname();
std::string getString();
std::string EString();
void setFname(std::string f_name);
void setLname(std::string l_name);
void setHRate(double hourlyrate);
void setHWorked(double hoursworked);
friend std::ostream &operator<< (std::ostream &obj, Employee str);
};