Calculate monthly gross pay

I am attempting to calculate the hours and rate for the week for four weeks and then calculate the gross pay of all four weeks for a monthly total. I am not able to get the program to calculate the monthly. Any help would be great.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 



#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;


double calcTotalGrossPay (double rate, double hours)
{
return rate * hours;
}
int main()
{

const double OVERTIME_RATE = 1.5;

// declare variables

double hours;
double rate;
double gross;
int weekBeingPaid = 1;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision (2);


    cout << "Enter employee's hourly rate: ";
    cin >> rate;

//do while loop to get the hourly rate

gross = gross + calcTotalGrossPay (rate, hours);
     while (weekBeingPaid <=4)

{
        cout << "Enter hours worked for week  " << weekBeingPaid<<":" <<endl;
        cin >> hours;



// while loop for hour


// calculate pay

    if (hours > 40)

{


        gross = (40* rate) + (hours - 40) * OVERTIME_RATE;

        gross = gross = calcTotalGrossPay (hours, rate);
}

    else if (hours <= 40)

{

        gross = hours * rate;
        gross = gross = calcTotalGrossPay (hours, rate);
}

    else if (gross + hours > 40)
{


     gross = gross + calcTotalGrossPay (hours, rate);


}

cout <<"Employee's pay for the week "<<weekBeingPaid<<" is: "<< gross <<endl;

 weekBeingPaid++;

}

cout << "Employee's total gross pay for the month is: "<< calcTotalGrossPay <<endl;

// Display results





    return 0;
}













I added some comments to get you pointed in the right direction, let me know if any of them don't make sense to you

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
50
51
52
53
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

double calcWeekPay (double rate, double hours)
{
    //In this function return the pay for the week, not months gross (check for overtime) 
}

int main()
{

    const double OVERTIME_RATE = 1.5;

    // declare variables
    
    double hours = 0;
    double rate = 0;
    double gross = 0; //Don't forget to initialize this variable or you might end up with garbage values.
    double weekPay = 0; //add a variable for the current weeks pay
    int weekBeingPaid = 1;
    
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision (2);

    cout << "Enter employee's hourly rate: ";
    cin >> rate;

    //while loop to get the weekly amounts
    while (weekBeingPaid <=4)
    {
        cout << "Enter hours worked for week  " << weekBeingPaid<< ": ";
        cin >> hours;
    
        // calculate pay
        //Here run your calcWeekPay function and return its value to weekPay
        
        //add the week pay to your months gross
        
        cout <<"Employee's pay for the week "<<weekBeingPaid<<" is: "<< weekPay <<endl;
    
        weekBeingPaid++;

    }

    cout << "Employee's total gross pay for the month is: "<< gross <<endl;

    // Display results

    return 0;
}
//I included the variable weekly and changed the gross to monthly to make more sense. I attempted to put weekly (weekPay) to add the week pay to the month gross but calculations were off. So my overtime is not calculating and monthly gross is not adding up. Still confused on what I am doing wrong. Thank you for your helpl.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

const double OVERTIME_RATE = 1.5;


double calcTotalGrossPay (double rate, double hours)

{

return rate * hours ;

}

int main()

{

// declare variables

double hours = 0;
double rate = 0;
double monthly = 0;
double weekly = 0;
int weekBeingPaid = 1;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision (2);


    cout << "Enter employee's hourly rate: ";
    cin >> rate;

//do while loop to get the hourly rate


monthly = monthly + calcTotalGrossPay (rate, hours);


     while (weekBeingPaid <=4)

{

        cout << "Enter hours worked for week  " << weekBeingPaid <<":" <<endl;

        cin >> hours;

// while loop for hour

// calculate pay

    if (hours > 40)

{

        weekly = (hours * rate) + (hours - 40) * OVERTIME_RATE;

        monthly = monthly = calcTotalGrossPay (hours, rate);

}

    else if (hours <= 40)

{


        weekly = hours * rate;

        monthly = monthly = calcTotalGrossPay (hours, rate);

}

    else if (monthly + hours > 40)

{

     monthly = monthly + calcTotalGrossPay (hours, rate);


}

cout <<"Employee's pay for the week "<<weekBeingPaid<<" is: "<< monthly <<endl;

 weekBeingPaid++;

}

cout << "Employee's total gross pay for the month is: "<< calcTotalGrossPay <<endl;


// Display results

    return 0;

}
I also received a warning on line 63. Operation on monthly may be undefined.
Line 62 and line 73, both have the same type of error. You can't have 2 equal signs in the same line. Change the second equals to a +, like you have on lines 41 and 81.
line 90 should be monthly variable . Because of the loop the amount monthly doubles on each iteration , So a quick and dirty fix is to divide by 2 monthly/2.
Last edited on
I added a few things and overtime and regular time is calculating for the week correctly. The calculations for the month is not correct.
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;




double calcTotalGrossPay (double rate, double hours)

{

return  rate * hours;

}

int main()

{

// declare variables
double overtime = 1.5;
double hours = 0;
double rate = 0.0;
double othours= 0;
double otpay = 0.0;
double weeklyhrs = 0;
double weeklypay = 0.0;
double weekly = 0.0;
int weekBeingPaid = 1;
int monthly = 0.0;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision (2);


    cout << "Enter employee's hourly rate: ";
    cin >> rate;

//do while loop to get the hourly rate


monthly = monthly + calcTotalGrossPay (rate, hours);


     while (weekBeingPaid <=4)

{

        cout << "Enter hours worked for week  " << weekBeingPaid <<":" <<endl;

        cin >> hours;

        cout << "Total hours worked for week is: " << hours<< "."<< endl;

// while loop for hour

// calculate pay

    if (hours > 40)

{
        othours = (hours - (hours - 40));

        otpay = ((hours - 40) * (overtime * rate));

        weeklyhrs = 40;

        weeklypay =  40 * rate;

        weekly = ((hours - 40) * (overtime * rate)) + (othours * rate) ;

        monthly = weeklypay + otpay;

        monthly = monthly + calcTotalGrossPay (hours,rate);


}

    else if (hours <= 40)

{

        weekly = hours * rate;

        monthly = weekly;

        monthly = monthly + calcTotalGrossPay (hours, rate);

}

    else if (weekly + othours > 40)
    {

        othours = (hours - (hours - 40));

        weekly =  ((hours - 40) * (overtime * rate)) + (othours * rate) ;

        weekly = weeklypay + otpay  ;

        monthly = weekly ;

        monthly = monthly + calcTotalGrossPay (hours, rate);

    }


cout <<"Employee's pay for the week "<<weekBeingPaid<<" is: "<< weekly<<endl;

 weekBeingPaid++;

}

cout << "Employee's total gross pay for the month is: "<< weekly + monthly + calcTotalGrossPay(hours, rate)<<endl;


// Display results

    return 0;

}
It seems like your headed in the right direction (although there may be some unnecessary calculations/variables in there, but we can handle that later).

Take some time and check every case, what if you work only 30 hours every week, what if you work 40 hours every week, what if you work 45 hours every week. Figure out what your answer for the weekly calculations should be and see if that is correct. Figure out what your answer for the total months calculation should be and check if that is correct. Testing each of these cases will help you narrow down the case where your calculations are not doing what you think they are doing.

Simplify as much as possible, its a lot easier to make mistakes when you make the same calculation in various places instead of just one place.
HINT: Don't update the monthly pay in each of the if/else if blocks, wait until you have calculated the weekly pay in those blocks then after just set monthly to monthly + the weekly pay just calculated.
Last edited on
Topic archived. No new replies allowed.