Please take a look at my function call. I don't know if I'm handling the return values right. It works for an employee with less than or equal to 40 hours worked, but the over time is paying 2.5 times instead of 1.5 times. Should I be using some kind of alias for handling the struct in the functionion calls and returns? thanks

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

struct worker {
    int    idNumber;
    int    hoursWorked;
    double hourlyRate;
    double earned;
};
[B]
worker [/B]inputData(int, int, double);[B]
worker calculatePay(worker);[/B]
void outputPay(worker);

const double OVERTIME_RATE = 1.5;

int main() {
    
    int id, hrs, rate;
    cout << "Enter employee ID number, hours worked, and pay rate: ";
    cin >> id >> hrs >> rate;
    
   [B] outputPay( calculatePay( inputData(id, hrs, rate) ) );  [/B]  

    return 0;
}

worker inputData(int number, int hrsWorked, double rate) {
    worker w;
    w.idNumber = number;
    w.hoursWorked = hrsWorked;
    w.hourlyRate = rate;
    
    return w;

}

worker calculatePay(worker emp) {

    double pay = static_cast<double>(emp.hoursWorked) * emp.hourlyRate 
                + OVERTIME_RATE * emp.hourlyRate * static_cast<double>(emp.hoursWorked - 40);
    emp.earned = pay;
    return emp;
}

void outputPay(worker idNum) {

    cout << endl << setw(10) << "employee" << setw(10) << "hours" << setw(10) 
                    << "rate" << setw(10) << "pay" << endl << endl;
                    
    cout << fixed << setprecision(2) << setw(10) << idNum.idNumber << setw(10) << idNum.hoursWorked << setw(10)
        << idNum.hourlyRate << setw(10) << idNum.earned << endl;

}

Enter employee ID number, hours worked, and pay rate: 12 41 10

employee hours rate pay

12 41 10.00 425.00

Dude see the input u have given, its 41 hrs and the rate per hour is 10.

So do the simple math,

normal rate = 41 * 10 = 410

overtime = (41 - 40) * 1.5 * 10 = 15

total sal = 410 + 15 = 425

? wat else u expected.

double pay = static_cast<double>(emp.hoursWorked) * emp.hourlyRate +
 OVERTIME_RATE * emp.hourlyRate * static_cast<double>(emp.hoursWorked - 40);

A couple of problems. First, you're paying the employee for 41 hours at the normal rate plus their overtime pay. This equals 41*10 + 1.5*10*1 = 425. Secondly, what happens if the worker works for less that 40 hours?

thanks, and i appreciate the condescension from the first "dude". those who never make simple mistakes really impress everyone.

This article has been dead for over six months. Start a new discussion instead.