3
\$\begingroup\$

This program is a payroll using calculate two types of employee. Two overloaded functions for two type of employee (hourly and salaried). There must be one pass-by-value, one pass-by-reference, and two pass-by-reference using pointer. All the variable must be local. There are 3 files of this project.

Interface

    class PayRoll
    {
    public:
        PayRoll();
        void choice();
        void gross(double&);
        void gross(double, double);
        double calcFed();
        double calcState();
        double calcNetPay(double, double);
    };

Implementation

#include <iostream>
#include "Payroll.h"
#include <iomanip>
#include <stdlib.h>
#include <string>
using namespace std;
double grossPay;
PayRoll::PayRoll()
{
    double fedRate;
    double stateRate;
    cout << "Enter the Federal Tax Rate in % \n";
    cin >> fedRate;
    while (fedRate < 0)
    {
        cout << "Please Enter a valid number:\n";
        cin >> fedRate;
    }
    cout << "Enter the State Tax Rate in % \n";
    cin >> stateRate;
    while (stateRate < 0)
    {
        cout << "Please Enter a valid number:\n";
        cin >> stateRate;
    }
}

void PayRoll::choice()
{
    cout << "Enter 1 - Calculate payroll for hourly employee" << endl
         << "Enter 2 - Calculate payroll for salary employee" << endl
         << "Enter 3 - Exit" << endl;
    char choice = 0;
    while (choice != '3')
    {
        cout << "\nEnter your choice: " << endl;
        cin >> choice;
        string lastName;
        string firstName;
        switch (choice)
        {
        case '1':
            double hours;
            double payrate;
            cout << "Enter employee Last Name: " << endl;
            cin >> lastName;
            cout << "Enter employee First Name: " << endl;
            cin >> firstName;
            if (firstName.length() <= 1)
            {
                firstName = true;
            }
            else
            {
                firstName = firstName.substr(0, 1);
            }
            cout << "Enter hours: " << endl;
            cin >> hours;
            cout << "Enter payrate: " << endl;
            cin >> payrate;
            payrate = (payrate);
            gross(hours, payrate);

            cout << "Employee: " << lastName << ", " << firstName << endl;
            cout << setprecision(2) << fixed;
            cout << "Gross Pay: " << grossPay << endl 
                 << "Federal Tax: " << calcFed() << endl 
                 << "State Tax: " << calcState() << endl 
                 << "Total Tax: " << calcFed() + calcState() << endl 
                 << "Net Pay: " << grossPay - (calcFed() + calcState()) << endl;
            break;

        case '2':

            double salary;
            cout << "Enter employee Last Name: " << endl;
            cin >> lastName;
            cout << "Enter employee First Name: " << endl;
            cin >> firstName;
            if (firstName.length() <= 1)
            {
                firstName = true;
            }
            else
            {
                firstName = firstName.substr(0, 1);
            }
            cout << "Enter salary: " << endl;
            cin >> salary;
            salary = (salary);
            gross(salary);

            cout << "Employee: " << lastName << ", " << firstName << endl;
            cout << setprecision(2) << fixed;
            cout << "Gross Pay: " << grossPay << endl
                 << "Federal Tax: " << calcFed() << endl
                 << "State Tax: " << calcState() << endl
                 << "Total Tax: " << calcFed() + calcState() << endl
                 << "Net Pay: " << grossPay - (calcFed() + calcState()) << endl;
            break;
        case '3':
            cout << "Exited" << endl;
            break;
        default:
                cout << "Please try again" << endl;
                break;      
        }
    }
}

void PayRoll::gross(double hours, double payrate)
{
    if (hours > 0 && hours <= 40)
        grossPay = hours * payrate;
    if (hours > 40 && hours <= 50)
        grossPay = (40 * payrate) + ((hours - 40) * (payrate * 1.5));
    if (hours > 50)
        grossPay = ((40 * payrate) + ((10) * (payrate * 1.5)) + (hours - 50) * (payrate * 2));
}

void PayRoll::gross(double&salary)
{
    grossPay = salary / 52;
}

double PayRoll::calcFed()
{
    double fedRate = 10;
    double* nPtr(&fedRate);
    double FedTax = grossPay * (*nPtr / 100);
    return FedTax;
}

double PayRoll::calcState()

{
    double stateRate = 5;
    double* pPtr(&stateRate);
    double StateTax = grossPay * (*pPtr / 100);
    return StateTax;
}
double PayRoll::calcNetPay(double FedTax, double StateTax)

{
    double NetPay = grossPay - (FedTax + StateTax);
    return NetPay;
}

Test

#include "PayRoll.h"
int main()
{
    PayRoll myPayRoll;
    myPayRoll.choice();
}

I think I did something wrong on local variables but I have no ideas how to fix it. Second, I'm not really sure about pass-by-reference using pointer.

\$\endgroup\$
4
  • \$\begingroup\$ Does this program work correctly, or does it have erroneous output? If it works correctly, we will be glad to help you improve it, but if it does not, you may post it on Stack Overflow. If you suspect problems with your use of pass-by-value and pass-by-reference, there are very good articles about the difference around the web. \$\endgroup\$
    – user34073
    Commented Apr 2, 2015 at 21:59
  • \$\begingroup\$ This project works correctly so far. I really need someone to help me review the code before I turn it in. \$\endgroup\$
    – Jin Nguyen
    Commented Apr 2, 2015 at 22:02
  • \$\begingroup\$ @JinNguyen What does this mean? "I think I did something wrong on local variables but I have no ideas how to fix it." \$\endgroup\$
    – nhgrif
    Commented Apr 2, 2015 at 22:24
  • \$\begingroup\$ @JinNguyen Would you mind telling us when this homework assignment is due? \$\endgroup\$
    – nhgrif
    Commented Apr 2, 2015 at 22:35

1 Answer 1

3
\$\begingroup\$

using namespace std; - not a very good practice because namespaces can have functions and classes with the same names, which become ambiguous and can cause various problems: Why is using namespace std; considered bad practice.

You can clean your code up by adding more functions. Why don't you create a function to output the employee data:

void outputEmployeeData()
{
    std::cout << "Employee: " << lastName << ", " << firstName << std::endl;
    std::cout << setprecision(2) << fixed;
    std::cout << "Gross Pay: " << grossPay << std::endl 
         << "Federal Tax: " << calcFed() << std::endl 
         << "State Tax: " << calcState() << std::endl 
         << "Total Tax: " << calcFed() + calcState() << std::endl 
         << "Net Pay: " << grossPay - (calcFed() + calcState()) << std::endl;
}

You will need to pass the data in to this method.

You can also clean your code up by adding private variables and methods to the Payroll class:

class PayRoll
{
public:
    PayRoll();
    void choice();
    void gross(double&);
    void gross(double, double);
    double calcFed();
    double calcState();
    double calcNetPay(double, double);

private:
    double grossPay;
};

Now, you don't need to have a global variable in your implementation file.

I would probably create an Employee class and maybe have a vector of employees in the payroll. You could then have functions to add and remove a given employee, and you could just pass the output method an Employee variable that it could pull all the data from.

class Employee 
{
public:
    Employee(string fName, string lName, int wage);
    string getFirstName();
    string getLastName();
    void setWage(int);    // PLEASE don't use doubles for money, use ints
private:
    string firstName;
    string lastName;
    int wage;
};

Here is the updated Payroll class:

class PayRoll
{
public:
    PayRoll();
    void choice();
    void gross(double&);
    void gross(double, double);
    double calcFed();
    double calcState();
    double calcNetPay(double, double);
    void addEmployee(Employee);
    void removeEmployee(Employee);

private:
    std::vector<Employee> employees;
};

Warning:

I did not write this in an IDE, nor did I test it. It may not compile without modification.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.