[Solved] Payroll program in C++


Initialize employeeCounter and it’ll work:

int employeeCounter = 0;

Frankly, this code simply is not presentable at all. You need to at least indent it consistently. There are a lot of things that you need to look at.

You should go through the C++ Core Guidelines.

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md

Edited — Updated Code

Here’s your cleaned up and functional code:

#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;

class employee
{
public:
    double salary, hourlyRate, taxRate, taxAmount, grossPay, netPay, otPay;
    int hours, otHours;

    int payStat;
    int employeeID;
    string firstName;
    string lastName;

public:
    void setVariables( int empID, string fName, string lName, int stat, int rate, int hrs )
    {
        employeeID = empID;
        firstName = fName;
        lastName = lName;
        payStat = stat;

        if( payStat == 1 )
        {
            hourlyRate = rate;
        }
        else
        {
            salary = rate;
        }

        hours = hrs;
    }

public:
    virtual double calculateGrossPay() = 0;

    double calculateTaxAmount()
    {
        taxRate = .30;
        taxAmount = grossPay*taxRate;
        return taxAmount;
    }

    double calculateNetPay()
    {
        netPay = grossPay - taxAmount;
        return netPay;
    }

    void printData()
    {
        cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint);
        cout << firstName << setw(6) << lastName << setw(6) << employeeID << setw(10) << hours << setw(3)
             << otHours << setw(8) << grossPay << setw(8) << netPay << setw(8) << otPay << endl;
    }
};

class employeeSalary : public employee
{
public:
    double calculateGrossPay()
    {
        double regPay = hours*hourlyRate;
        double hourlyRate = ((salary/52)/40);
        if (hours > 40)
        {
            otHours = (hours - 40);
            otPay = (otHours * hourlyRate);
            grossPay = (regPay + otPay);
        }
        else if (hours <= 40)
        {
            otHours = 0; otPay = 0; grossPay = regPay;
        }

        return grossPay;
    }
};

class employeeHourly : public employee
{
public:
    double calculateGrossPay()
    {
        const double regPay = (40 * hourlyRate);

        if ( hours > 40 )
        {
            otHours = (hours - 40);
            otPay = (otHours * hourlyRate * 1.5);
            grossPay = (regPay + otPay);
        }
        else
        {
            otHours = 0;
            otPay = 0;
            grossPay = regPay;
        }

        return grossPay;
    }
};

int main()
{
    int employeeCounter = 0;
    int totalEmployeeCount = 0;

    string fName, lName;
    int empID = 0, stat = 0, rate = 0, hrs = 0;

    cout << "enter # of employees you want to process:  ";
    cin >> totalEmployeeCount;

    employee* employee[100];

    while( employeeCounter < totalEmployeeCount )
    {
        cout<<"Is employee "<< employeeCounter+1 << " hourly or salary? (enter 1 for hourly / 2 for salary):";
        cin>>stat;

        if (stat == 1)
        {
            cout << "Instantiating and HOURLY employee object inherited from base class employee" << endl << endl;

            cout<<"Enter employee's ID: ";
            cin>>empID;
            cout<<"Enter employee's first name: ";
            cin>>fName;
            cout<<"Enter employee's last name: ";
            cin>>lName;
            cout<<"Enter employee's hourly wage: ";
            cin>>rate;
            cout<<"Enter employee's hours for this week: ";
            cin>>hrs;

            employee[employeeCounter] = new employeeHourly();
            employee[employeeCounter]->setVariables( empID, fName, lName, stat, rate, hrs );
            employee[employeeCounter]->calculateGrossPay();
            employee[employeeCounter]->calculateTaxAmount();
            employee[employeeCounter]->calculateNetPay();
            cout<<endl<<endl;
            employeeCounter++;
        }
        else
        {
            cout<<"instantialting a SALARY employee object in herited from base class employee"<<endl<<endl;
            cout<<"Enter employee's ID: ";
            cin>>empID;
            cout<<"Enter employee's first name: ";
            cin>>fName;
            cout<<"Enter employee's last name: ";
            cin>>lName;
            cout<<"Enter employee's hourly wage: ";
            cin>>rate;
            cout<<"Enter employee's hours for this week: ";
            cin>>hrs;
            employee[employeeCounter] = new employeeHourly();
            employee[employeeCounter]->setVariables(empID, fName, lName, stat,
                                                    rate, hrs);
            employee[employeeCounter]->calculateGrossPay();
            employee[employeeCounter]->calculateTaxAmount();
            employee[employeeCounter]->calculateNetPay();
            cout<<endl<<endl;
            employeeCounter++;
        }
    }

    cout << "-----------------------------------------\n";

    for ( int i = 0; i < employeeCounter; ++i )
    {
        employee[ i ]->printData();
    }

    cout << "-----------------------------------------\n";

    return 0;
}

You had global variables to store the data and in your print function you were printing those globals. That means you’d only get the latest stored values.

You really need to go through the fundamentals of OOP to get those things right.

Always indent your code.
Initialize your variables.
And, don’t comment each line of code.

5

solved Payroll program in C++