0

Calculating four weeks pay.

I am trying to read four weeks worth of hours from an employee, apply the calcPay function, and total the four weeks to display gross pay for the month. In my code, the calcPay function is only applying to the last week's entry. How do I get it to apply to all four weeks and display the gross pay total? Thanks. //*************************************************** //Determine gross pay for a four-week period based //on hourly rate and number of hours worked each week // with any hours over 40 being paid time-and-a-half //*************************************************** #include <iostream> void calcPay (double, double, double&); //Declaring a function before using & pointer to return const float MAX_HOURS = 40.0; const float OVERTIME = 1.5; using namespace std; int main() { double payRate = 0.0; int weeks = 4; double hours[i] = {0.0}; double wages = 0.0; cout << "Enter pay rate amount:

quot;; cin >> payRate; for(int i=0; i<weeks; i++) { cout<<"Enter "<<(i+1)<<" week working hours: "; cin >> hours[i]; } calcPay(payRate, hours, wages); cout <<"Wages earned this month are " << wages << "."; return 0; } void calcPay (double payRate, double hours, double& wages) { if (hours > MAX_HOURS) { wages = MAX_HOURS * payRate + (hours - MAX_HOURS) * OVERTIME * payRate; } else { wages = hours * payRate; } return; }

28th Nov 2017, 1:06 AM
Elyse Segebart
Elyse Segebart - avatar
1 Resposta
+ 1
You have multiple errors. 1) double hours[i] = {0.0}; i is undefined. weeks appears to be what you want, based on for loop later on. You could avoid initializing here as for loop will do it later. 2) calcPay(payRate, hours, wages); void calcPay(double payRate, double hours, double& wages) You are only passing in the first array element as the function only expects one. Change to double hours[]. 3) function needs to loop through each element of hours as part of the calculation.
28th Nov 2017, 1:38 AM
John Wells
John Wells - avatar