Get the program to apply time and a half to all weeks.

In my code, I am trying to enter four weeks worth of hours and get the weekly total and then total all four weeks for a gross pay for the month. My code applies the calcPay function to only the last week. How do I get it to apply it to the rest of the weeks?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//***************************************************
//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 = 0.0;
double wages = 0.0;

cout << "Enter pay rate amount: $";
cin >> payRate;

for(int i=0; i<weeks; i++)
    {
    cout<<"Enter "<<(i+1)<<" week working hours: ";
    cin >> hours;
    }

    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;
}
Last edited on
At lines 25-29 you have a loop where the hours are entered. Inside that loop, just after getting the hours, call the calcPay() function.

But that won't be enough by itself. Each time it is called, it will generate a resulting value of wages.

I'm not sure what you want to do next. Perhaps print out the wages for each week inside that same loop. Or perhaps add it to a grand total and then after the loop is completed, print out the total for all four weeks. Or maybe both.
Last edited on
Topic archived. No new replies allowed.