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);

};

Recommended Answers

All 4 Replies

Hi Smcp :)

Segmentation fault means that you are trying to read or write memory you do not have access to and that is exactly what is happening in you main-function.

You have created two arrays fName and lName containing five elements each. No problem there.

In your for-loop you access the arrays like this:
fName[fRandom] and lName[lRandom]

The problem is that you set the fRandom and lRandom variables to random values between 0 and 9.

Remember that you only have 5 element in the arrays.

Add more elements to the arrays or change the random values to:
rand() % 5; //Instead of 10

Awesome, that cleared up the segmentation fault but now I have another problem that I can't seem to figure out the cause of. When all of the employee information prints, it isn't printing out any names (first or last) or the ID.

When I run my program, this is the output I get:

ID First Name Last Name Hours Worked Hourly Rate Gross Pay Tax Net Pay

$20 $1380 30% $966
$21 $504 30% $352.8
$20 $1230 30% $861
$27 $2268 30% $1587.6
$23 $1587 30% $1110.9

All numbers and no names, any idea why?

Take a look at the EString-function where you call getHRate().

The next line reads:

str=+"\t $" + oss. str();

A minor typo. =+ should have been +=.

You sir are my hero, it's always little things like that that just drive me crazy because I tend to glance right over them. Everything is working perfectly now thanks to you, I really appreciate the help, thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.