Need Help with C++ calculate hr/rt/wk/mo/gross pay

Been in class for two weeks. I am not sure what to do next. I think I need a for loop. Can anyone help me out if that is what I need and how I would do it? C++ console application that asks the user to enter the hourly wage and number of hours worked for each week in a 4-week pay period. Create a custom function with appropriate input parameters and return type that calculates the total gross pay based on the values entered by the user. Use suitable loops for the user input and the processing of data entered by the user. The program should print out the gross pay for the pay period for the employee.



#include <iostream>

using namespace std;

int main()

{

double PayRate, HoursWorked, GrossPay, OTPay;

cout << "Input hourly wage rate..";
cin >> PayRate;
cout << endl;
cout << "Input hours worked this week..";
cin >> HoursWorked;
cout << endl;


if( HoursWorked > 40)
{

OTPay = HoursWorked *1.5* PayRate;
GrossPay = OTPay - (HoursWorked * PayRate);
cout <<"Employees gross pay for this week is" << GrossPay<< endl;
}

else if (HoursWorked < = 40)
{

GrossPay = HoursWorked * PayRate;
cout <<"Employees gross pay for this week is" << GrossPay <<endl;
Last edited on
Please use code tags. http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post, highlight your code and click the <> button on the right.

1
2
3
4
5
6
7
8
9
10
11
12
if (HoursWorked > 40) {

    OTPay = HoursWorked * 1.5 * PayRate;
    GrossPay = OTPay - (HoursWorked * PayRate);
    cout << "Employees gross pay for this week is" << GrossPay << endl;
}

else if (HoursWorked < = 40) {

    GrossPay = HoursWorked * PayRate;
    cout << "Employees gross pay for this week is" << GrossPay << endl;
}

You can easily turn this into a function.
Also, your OTPay calculation is wrong. You forgot to subtract 40 from HoursWorked, to get how many hours they worked overtime.
Here's some boilerplate code for the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double CalcGrossPay( double PayRate, double HoursWorked )
{
    const double FULLTIME_HRS = 40.0;

    // check for valid input
    // PayRate and HoursWorked must be > 0

    double GrossPay = 0.0;
    if( HoursWorked > FULLTIME_HRS ) {

    }
    else if( HoursWorked <= FULLTIME_HRS ) {

    }

    return GrossPay;
}
This is what I have running errors.

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>          //Header file meaning Input/Output Stream

#include <iomanip>           //Affects the state of iostream objects



using namespace std;



int calcWeek (Week weeks []); // Pay data can be stored for four weeks

double calcRegPay (Week); // This will calculate regular pay

double calcOtPay (Week); //This will calculate Over Time Pay

double calcGrossPay (double, double); // This will calculate total gross pay



struct Week   //Stores pay data for one week



{

int hours;

double  rate;

};



static const int MAX_WEEKS = 4;   //  Number of weeks in a month





int main() //main function



//Declare variable block

int weeks_worked;

double  regPay = 0;

double  otPay = 0;

double  totalGrossPay = 0;



double calcRegPay (Week week)  //Will calculate regular pay for one week

{   return week.hours * week.rate;

}



double calcOtPay(Week week)    //Will calculate the overtime pay for one week

{   return (week.hours - 40) * (week.rate * 1.5) + (week.rate * 40);

}



double calcGrossPay (double regPay, double otPay)

{   return regPay + otPay;

}



return MAX_WEEKS;





// Input Statements Block



 int calcWeek (Week weeks[])



   for (int x = 0; x <MAX_WEEKS; x++)

    {   cout << "Please Enter Following Information:  " << (x + 1) << endl;

               cout << "______________________________" << endl;

               cout << endl;



               cout << "Employee Hours: ";

               cin >> weeks[x].hours;

               cout << endl;

               if (weeks[x].hours == 0)

                   return x;   //  Number of weeks entered

        cout << "Employee Pay Rate: ";

        cin >> weeks[x].rate;

        cout << endl;  }

// Conditional Statement Block




    for (int x=0; x <weeks_worked; x ++)

    {   if (weeks[x].hours > 40)

otPay = (regPay - 40) * 1.5 * regPay (weeks[x]);

 else

  { (weeks[x].hours <= 40)

 regPay += calcRegPay (weeks[x]);







 totalGrossPay += calcGrossPay (regPay, otPay);  // Add weekly pay to the gross pay.

    };

    cout << "____________________________________________" << endl;

    cout << "The Employee’s Total Gross Pay for the Month is: $" << setprecision(2) << fixed << totalGrossPay << " %%%" << endl;

    cout << "_____________________________________________" << endl;


}





{   Week    weeks[MAX_WEEKS];                           //  4 weeks worth of data





 weeks_worked = calcWeek (weeks);   //  This will calculate pay for one week



    return 0;

}
Last edited on
Topic archived. No new replies allowed.