About C++ Payroll

Regarding your code snippet:

if (Hours <= 40);
    RegularPay = Hours * PayRate;
    OvertimePay = 0;

This doesn’t do what you think. The ; at the end of the if statement means “if hours is less than 40, do nothing”, then it sets regular and overtime regardless. What you probably wanted was:

if (Hours <= 40) {
    RegularPay = Hours * PayRate;
    OvertimePay = 0;
}

The whole calculation of regular and overtime pay can probably be better written as:

if (Hours <= 40) {
    RegularPay = Hours * PayRate;
    OvertimePay = 0;
} else {
    RegularPay = 40.0 * PayRate;
    OvertimePay = 1.5 * (Hours - 40) * PayRate;
}

GrossPay = RegularPay + OvertimePay;
std::cout << "Gross Pay is = $"  << GrossPay << '\n';

You can see that the correct values are set for regular and overtime pay for the two situations, after which you can add them and print them in any way you want.

Keep in mind this (the use of RegularPay = 40.0 * PayRate in the else clause) is for overtime being time-and-a-half as that’s often the case.

If you work in an industry where it’s double-time-and-a-half (i.e., you’re very lucky), change the calculation to RegularPay = Hours * PayRate as per your original. That seems to be the way your description specifies it but you may want to check with your tutor or at least comment the reasoning.

If it is double-time-and-a-half, you can simplify the code to be something like:

RegularPay = Hours * PayRate;
OvertimePay = 0;
if (Hours > 40)
    OvertimePay = 1.5 * (Hours - 40) * PayRate;

GrossPay = RegularPay + OvertimePay;
std::cout << "Gross Pay is = $"  << GrossPay << '\n';

Leave a Comment