0

I have been trying to do debug this for a long time using visual studio debugger but I can't figure out why my function emp.setHoursWorked(hWorked); in recordHoursWorkedForEmployee only seems to be update numOfHoursWorked while in recordHoursWorkedForEmployee, as soon the program exits the function the numOfHoursWorked of all Employees in the vector go back to 0. Below is the code in question. Any help would be appreciated.

#ifndef PAYROLLSYSTEM   
#define PAYROLLSYSTEM
#include "Employee.h"
#include "Paycheck.h"
#include <string>
#include <vector>

using namespace std;

class PayRollSystem
{
public:

    //custom constructor gets company name
    PayRollSystem(string);
    void createEmployee(string, string,string, double);
    void removeEmployee(string);
    void recordHoursWorkedForEmployee(string);
    void issuePaychecks();


private:
    string companyName;
    vector<Employee> companyEmployees;
};
#endif



void PayRollSystem::createEmployee(string empId, string fName, string lName, double hWage)
{
    Employee temEmp = Employee(empId, fName, lName, hWage);
    companyEmployees.push_back(temEmp);

}


void PayRollSystem::recordHoursWorkedForEmployee(string empId)
{

    for (Employee emp : companyEmployees)
    {

        if (emp.getEmployeeId() == empId)
        {
            int hWorked = 0;
            cout << "What are the hours the worked for " + emp.getEmployeeId() + " during current pay period?" << endl;
            cin >> hWorked;

            //TODO: For some reason this line is not updating the hours worked. Must fix!
            emp.setHoursWorked(hWorked);
            cout << "Hours for " + emp.getEmployeeId() + " have been changed to " << emp.getHoursWorked() << endl;
        }
    }

}

I excluded the header file here in order to not paste too many things not relevant to the problem i'm facing, only implementations of member functions relevant to the problem provided

//Overloaded constructor to be used with PayRollSystem
Employee::Employee(string empId, string fName, string lName, double hWage)
{
    employeeId = empId;
    firstName = fName;
    lastName = lName;
    hourlyWage = hWage;
    numOfHoursWorked = 0;

}

void Employee::setHoursWorked(int hWorked)
{
    if (hWorked >= 0)
        numOfHoursWorked = hWorked;
    else
    {
        cout << "Invalid number of hours worked." << endl;
        exit(EXIT_FAILURE);
    }
}

string Employee::getEmployeeId() const
{
    return employeeId;
}
Squanchy
  • 103
  • 1
  • 2
  • 13

1 Answers1

3

This line makes a copy of each employee:

for (Employee emp : companyEmployees)

The variable emp is a copy of the object in the container. So if you update this you are only updating the copy. Each iteration you get a new value copied into emp but any changes are not reflected in the original object.

You probably meant:

for (Employee& emp : companyEmployees)
            ^^^

Here emp is a reference to the object inside the vector. If you modify this you are modifying the original value inside the vector.

Martin York
  • 257,169
  • 86
  • 333
  • 562