C++/intro to programming
Expert: Zlatko - 8/20/2010
QuestionI tried to write a code for this program but I have no luck to run it. I need you to help me. Develop a C++ program that uses a while statement to determine the gross pay for each of several employees. The company pays "straight time" for the first 40 hours worked by each employee and pays "time-and-a-half" for all hours worked in excess of 40 hours. you are given a list of the employees of the company, the number of hours each employee worked last week and the hourly rate of each employee. Your program should input this information for each employee and should determine and display the employee's gross pay. The gross pay should be printed as a floating-point value and with two digits of precision to the right of the decimal point.
Enter hours worked(-1 to end):39
Enter hourly rate of the worker(R00.00):10.00
Salary is R390.00
Enter hours worked(-1 to end):40
Enter hourly rate of the worker(R0.00):10.00
Salary is R400.00
Enter hours worked(-1 to end):41
Enter hourly rate of the worker(R00.00)
Salary is R415.00
Enter hours worked(-1 to end):-1
AnswerHello Mayibongwe
I would have liked to see your code, even if it didn't run. It would help me to help you learn. Anyway, I'll help you by starting you off. I'll show you how to do the input from the keyboard into variables, then you can use the variables in the salary calculation.
To do input and output in C++, you need to include the iostream header file, and use the iostream objects cin for input, and cout for output.
Below is the start of the code. You need to add the loop and the calculation.
Good luck, show me your attempt if you want more help
Kindest regards
Zlatko
#include <iostream>
using namespace std;
int main(void)
{
double hours;
double payRate;
double salary;
// TODO, put the code below into a loop
cout << "Enter hours worked(-1 to end):";
cin >> hours;
if (hours > 0)
{
cout << "Enter hourly rate of the worker(R00.00):";
cin >> payRate;
/* TODO calculate salary here based on the hours worked.
There are 2 cases, one where hours > 40,
and one where the hours are <= 40
*/
cout << "Salary is R" << salary << endl;
}
}