Home > database >  Pass by Reference for a pay calculation program
Pass by Reference for a pay calculation program

Time:10-19

I am tasked with writing functions and using pass by reference to send the data to the next functions to calculate data. The assignment was to take an employee number, pay code, and hours worked to determine total pay, and also determine if the given pay code was valid or not.

I'm having trouble with passing variable data from main() to the function code_not_valid(), and then code_not_valid() to get_pay(). Here is my source code:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>

const double MIN_WAGE = 9.87;
using namespace std;

void display(void);
bool code_not_valid(char&);
double get_pay(double, double&, double&);

int main()
{
    display();
    char paycode,yesno;
    int numberemp;
    double employeenumber = 0, hourly_pay = 0, hours, computed_pay = 0,sum;
    do {
        cout << "Enter your Employee number, paycode and hours worked";
        cin >> employeenumber, paycode, hours;
        code_not_valid(paycode);
        get_pay(employeenumber, hourly_pay, hours);
        cout << "Would you like to calculate the pay of another employee (Y/N) ";
        cin >> yesno;
        sum  = computed_pay;
        numberemp  ;
    } while ((yesno == 'Y') || (yesno == 'y'));
    cout << "\n The payroll for " << numberemp << " employees is $" << sum << endl;
}

void display(void)
{
    cout << "Lab 4b\n";
    cout << "Written by Drew Reif\n\n";
}

bool code_not_valid(char& paycode)
{
    bool done = false;
    while (!done) {

        double hourly_pay;
        if (paycode == 'M' || paycode == 'm') {
            hourly_pay = MIN_WAGE;
            done = true;
        }
        else if ((paycode == 'T') || (paycode == 't')) {
            hourly_pay = MIN_WAGE   1;
            done = true;
        }
        else if (paycode == 'O' || paycode == 'o') {
            hourly_pay = MIN_WAGE   2;
            done = true;
        }
        else {
            cin.clear();
            cin.ignore(10000, '\n');
            cout << "That is not a given pay code \n";
            cout << "Please try again.\n";
            done = false;
        }
    }
}

double get_pay(double employeenumber, double& hourly_pay, double& hours)
{
    double computed_pay;

    if (hours > 40) {
        computed_pay = ((hours - 40) * 1.5 * hourly_pay)   (40 * hourly_pay);
        std::cout << std::fixed;
        std::cout << std::setprecision(2);
        std::cout << "The computed pay is: $" << computed_pay;
        cout << "\n";
    }
    else if (hours <= 40) {
        computed_pay = hourly_pay * hours;
        std::cout << std::fixed;
        std::cout << std::setprecision(2);
        std::cout << "The computed pay is: $" << computed_pay;
        cout << "\n";
    }
}

CodePudding user response:

Your function get_pay doesn't return anything. You should modify it to

double get_pay(double employeenumber, double& hourly_pay, double& hours)
{
    double computed_pay = 0.0;

    // ... computations

    return computed_pay;
}

then in main you can call that function and assign the returned value

double computed_pay = get_pay(employeenumber, hourly_pay, hours);

Miscellaneous other comments:

  • employeenumber should be int instead of double (or some other integral type) if it is supposed to be an ID, it wouldn't make sense for an employee ID to be 4.127 for example
  • none of the arguments of code_not_valid nor get_pay need to be passed by reference as they are not modified within the function, and are primitive types so you don't need to avoid copies
  • you don't need to specify void for an argument accepting no arguments

CodePudding user response:

code_not_valid() is declared as returning a bool, but is not actually return'ing anything (same error with get_pay(), too).

For what you are attempting to do, code_not_valid() should take in the hourly_pay as a reference parameter, and then return true only if the paycode is valid and hourly_pay is updated, otherwise return false. Then you can have main() call get_pay() only when code_not_valid() returns true.

Also, all of your inputs and outputs should be in main(), not in your other functions. And main() has several variables that are not being initialized before they are used.

Try something more like this:

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

const double MIN_WAGE = 9.87;

void display();
bool is_code_valid(char, double&);
double get_pay(double, double);

int main()
{
    char paycode, yesno;
    int numberemp = 0;
    double employeenumber, hourly_pay, hours, computed_pay, sum = 0;

    display();

    do {
        do {
            cout << "Enter the Employee number, paycode, and hours worked: ";

            if (cin >> employeenumber >> paycode >> hours)
                break;

            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "That is not valid input. Please try again.\n";
        }
        while (true);

        while (!is_code_valid(paycode, hourly_pay)) {
            cout << "That is not a valid pay code. Please try again.\n";

            do {
                cout << "Enter the paycode for Employee " << employeenumber << ": ";

                if (cin >> paycode)
                    break;

                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "That is not valid input. Please try again.\n";
            }
            while (true);
        }

        computed_pay = get_pay(hourly_pay, hours);
        cout << fixed << setprecision(2)
             << "The computed pay is: $" << computed_pay << "\n";

        sum  = computed_pay;
          numberemp;

        cout << "Would you like to calculate the pay of another employee (Y/N) ";
        cin >> yesno;
    }
    while ((yesno == 'Y') || (yesno == 'y'));

    cout << "\n The payroll for " << numberemp << " employees is $" << sum << endl;
}

void display()
{
    cout << "Lab 4b\n";
    cout << "Written by Drew Reif\n\n";
}

bool is_code_valid(char paycode, double& hourly_pay)
{
    if (paycode == 'M' || paycode == 'm') {
        hourly_pay = MIN_WAGE;
        return true;
    }
    if ((paycode == 'T') || (paycode == 't')) {
        hourly_pay = MIN_WAGE   1;
        return true;
    }
    if (paycode == 'O' || paycode == 'o') {
        hourly_pay = MIN_WAGE   2;
        return true;
    }
    return false;
}

double get_pay(double hourly_pay, double hours)
{
    double computed_pay;

    if (hours > 40) {
        computed_pay = ((hours - 40) * 1.5 * hourly_pay)   (40 * hourly_pay);
    }
    else {
        computed_pay = hourly_pay * hours;
    }

    return computed_pay;
}
  • Related