I'm not sure on the correctness of your maths. I would calculate things differently. But let's put this aside and assume that your maths are correct. Your code is still not.
if (hrs_worked >= std_hrs) { /* ... */ } // regular + holiday hrs
else if (hrs_worked >= 40 && hrs_worked <= 60) { /* ... */ } // overtime
else if (hrs_worked >=60 && hrs_worked <=80) { /* ... */ } // double overtime
The above code of yours covers the various cases. However, it's wrong. Let's see some problems:
- You do not cover cases, when
- hrs_worked < std_hrs
- hrs_worked > 80
- You have multiple cases for the scenarios, when
- hrs_worked == std_hrs
- hrs_worked == 60
I think the first condition of yours should have been
if (hrs_worked <= std_hrs) { /* ... */ } // regular + holiday_hrs
The multiple cases can be sorted out by not checking equality in both the 2nd and 3rd conditions.
And last, but not least, I'd not set a high end limit (80) for double overtime.
Here's my take on the problem's solution:
#include <algorithm>
#include <iostream>
float reg_wages, overtime, double_overtime, holiday_wages, birthdaybonus_wages, total_hrs, total_wages;
const float std_hrs = 40.0;
const float overtime_hrs = std_hrs / 2;
using namespace std;
int main()
{
float hrs_worked;// attain total hrs_worked
cout <<"Enter hours worked for the week: ";
cin >> hrs_worked;
float hrly_rate;//attain regular hourly rate
cout <<"Enter hourly rate: $";
cin >> hrly_rate;
float holiday_hrs;//attain amount of holiday hrs
cout << "How many hours were holiday hours? ";
cin >> holiday_hrs;
total_wages = 0;
if (holiday_hrs > 0) // holidays worked
{
total_wages += holiday_hrs * 1.5 * hrly_rate;
hrs_worked -= holiday_hrs; // deduce the number of holidays from the hours worked. We've already payed for them.
}
if (hrs_worked < std_hrs) // standard hours worked
{
total_wages += hrs_worked * hrly_rate;
}
else // standard hours worked + ...
{
total_wages += std_hrs * hrly_rate;
hrs_worked -= std_hrs; // deduce the standard hours. We've already paied for them.
if (hrs_worked < overtime_hrs) // ... overtime worked
{
total_wages += hrs_worked * 1.5 * hrly_rate;
}
else // ... overtime worked + ...
{
total_wages += overtime_hrs * 1.5 * hrly_rate;
hrs_worked -= overtime_hrs; // deduce the overtime hours. We've already paied for them.
total_wages += hrs_worked * 2 * hrly_rate;
}
}
std::cout << "total wage: " << total_wages << std::endl;
return 0;
}