Hello, I was wondering how to pass an array to a class constructor. When I run my program, I get the same gross pay for every different employee even if I enter in a different number of hours for each one. That might be where I have a problem. Any help is appreciated! Here is my code:

#ifndef PAYROLL_H
#define PAYROLL_H

class Payroll
{
private:
    int hours_worked;
    double payRate;
    double grossPay;
public:

    Payroll()
    {
        hours_worked = 0;
        grossPay = 0.0;
        grossPay = 0.0;
    }

    Payroll(int hours, double rate = 7.25)
    {
        payRate = rate;
        hours_worked = hours;
    }

    double getGrossPay() const
    {
        return hours_worked * payRate;
    }
};
#endif


#include <iostream>
#include <iomanip>
#include "Payroll.h"

using namespace std;

int main()
{
    const int NUM_EMPLOYEES = 7;
    int hours[NUM_EMPLOYEES];
    double payRate;

    for (int index = 0; index < NUM_EMPLOYEES; index++)
    {
        cout << "Enter the number of hours worked by Employee # " << (index + 1) << ": ";
        cin >> hours[index];
    }

    Payroll employee(payRate, hours[6]);

    cout << "Here is the gross pay for each employee:\n";
    cout << fixed << showpoint << setprecision(2);
    for (int index = 0; index < NUM_EMPLOYEES; index++)
    {
        cout << "The gross pay for employee # " << (index + 1) << " is: " << employee.getGrossPay() << endl;
    }
    return 0;
}

Your problem is that you are only creating one Payroll employee item. line 51

Then from lines 55 -> 58 you are just looping over the single item that you have created therefore getting the same output for all employees.

An easy way to do this without passing an array to the class constructor is to use the STL list<>. Here is the working code using a list.

#include <iostream>
#include <iomanip>
#include "payroll.h"
#include <list>

using namespace std;
int main()
{
    const int NUM_EMPLOYEES = 7;
    list<Payroll*> employees; //-> define a list of payroll pointers
    Payroll * employee; // -> define a pointer
    int hours = 0; //-> variable for input
    const double payRate = 7.25;

    //int hours[NUM_EMPLOYEES]; -> list is replacing this array 


    for (int index = 0; index < NUM_EMPLOYEES; index++)
    {
        cout << "Enter the number of hours worked by Employee : ";
        cin >> hours;
        employee = new Payroll(hours, payRate);// -> create a new payroll item
        employees.push_back(employee);// -> add the item to the list
        hours = 0;
    }

    //Payroll employee(payRate, hours[6]); -> removed because we have create all the employees already

    cout << "Here is the gross pay for each employee:\n";
    cout << fixed << showpoint << setprecision(2);

    //define an interator for the list
    list<Payroll*>::iterator itr;

    for(itr = employees.begin(); itr != employees.end(); itr++)
    {
        cout << "The gross pay for employee is: " << (*itr)->getGrossPay() << endl;
    }

    system("pause");
    return 0;
}

In your "Payroll.h" i changed
Payroll(int hours, double rate = 7.25)
to
Payroll(int hours, double rate)

and made the payRate in the main loop const double payRate = 7.25
hope this helped.

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.