Calpay with loop

I created a program to calculate pay over 4 weeks. The program takes into cosideration that a work week is 40 hours and overtime after 40 hours. I having a hard time writing a loop. I think the loop is correct but not sure.

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
  #include <iostream>

using namespace std;

float getWage(float hours, float wage, float overtime){
   float regularHours;
   if (hours <= 40){
        regularHours = hours;
        overtime = 0;
    }
    else{
        overtime = hours - 40;
        regularHours = 40;
    }
    return (regularHours*wage + overtime * wage*1.5);
}

int main(){
   float hours, wage, overtime = 0, regularHours,sum = 0, salary;
   int count = 0, i = 0;
   while(i<4){
   cout<<"Enter hours worked: ";
   cin>>hours;
   cout<<"Enter pay rate: ";
   cin>>wage;
   if (hours <= 40){
        regularHours = hours;
        overtime = 0;
    }
    else{
        overtime = hours - 40;
        regularHours = 40;
        count++;
    }
    i++;
    salary = getWage(hours, wage, overtime);
    sum += salary;
    cout<<"Employee's Weekly paycheck: "<<salary<<endl;
   }
   cout<<"Total paid this month: "<<sum;
    return 0;
}
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
#include <iostream>
#include <iomanip>
using namespace std;

float getWage(float hours, float wage){
    float regularHours = hours, overtime = 0;
    if (hours > 40) {
        overtime = hours - 40;
        regularHours = 40;
    }
    return (regularHours + overtime * 1.5) * wage;
}

int main(){
    float sum = 0, wage;
    cout << fixed << setprecision(2);
    cout << "Enter pay rate: ";
    cin >> wage;
    for (int i = 0; i < 4; i++) {
        float hours;
        cout << "Enter hours worked for week " << i + 1 << ": ";
        cin >> hours;
        float salary = getWage(hours, wage);
        sum += salary;
        cout << "Employee's Weekly paycheck: " << salary << '\n';
    }
    cout << "Total paid this month: " << sum << '\n';
}

Topic archived. No new replies allowed.