About C++ Payroll -
i taking online c++ class , having hard time learning. not sure doing wrong code below problem. formula right if hours = 40, going wrong if hrs above 40 or below 40. appreciate help! cheers, r.
problem: if hrs <= 40 regular pay = hrs times pay rate if hrs > 40 overtime pay = 1.5 times (hrs - 40) times pay rate gross pay = regular pay plus overtime pay // code #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { //variable declarations int employeeidentificationnumber = 0; double hours = 0; double payrate = 0; double grosspay = 0; double regularpay = 0; double overtimepay = 0; std::cout << "welcome employee payroll.\n"; // display message std:: cout << "enter employee identification number: "; //promp user data std::cin >> employeeidentificationnumber; //read integer user employeeidentificationnumber std::cout << "please enter hours worked: " ; // prompt user data std::cin >> hours; //read integer user hours std::cout << "please enter pay rate: " ; // prompt user data std::cin >> payrate; //read integer user payrate regularpay = hours * payrate; //calculate regularpay overtimepay = 1.5 * (hours - 40) * payrate; //calculate overtime //qualifier regularpay if (hours <= 40); regularpay = hours * payrate; overtimepay = 0; grosspay = regularpay + overtimepay; std::cout << "gross pay = $" ; //qualifier overtime if (hours > 40); regularpay = hours * payrate; overtimepay = 1.5 * (hours - 40) * payrate; grosspay = regularpay + overtimepay; std::cout << regularpay + overtimepay << std::endl; std::cout << "thanks using employee payroll\n"; system("pause"); return exit_success;
}
regarding code snippet:
if (hours <= 40); regularpay = hours * payrate; overtimepay = 0;
this doesn't think. ;
@ end of if
statement means "if hours less 40, nothing", sets regular , overtime regardless. wanted was:
if (hours <= 40) { regularpay = hours * payrate; overtimepay = 0; }
the whole calculation of regular , overtime pay can 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 = $" << grosspay << '\n';
you can see correct values set regular , overtime pay 2 situations, after can add them , print them in way want.
keep in mind (the use of regularpay = 40.0 * payrate
in else
clause) overtime being time-and-a-half that's case.
if work in industry it's double-time-and-a-half (i.e., you're lucky), change calculation regularpay = hours * payrate
per original. seems way description specifies may want check tutor or @ least comment reasoning.
if is double-time-and-a-half, can simplify code like:
regularpay = hours * payrate; overtimepay = 0; if (hours > 40) overtimepay = 1.5 * (hours - 40) * payrate; grosspay = regularpay + overtimepay; std::cout << "gross pay = $" << grosspay << '\n';
Comments
Post a Comment