Added exercise 4.28
[Cpp-Deitel-Exercises.git] / Chapter-4 / 4-16.cpp
blobab4e8e24378972b755868bc3f299a24f9f4ef822
1 /*C++ How to Program, 9/E, by Paul Deitel & Harvey Deitel.
3 Solution of exercise 4.16:
4 (Salary Calculator) Develop a C++ program that uses a while statement to
5 determine the gross pay for each of several employees. The company pays
6 "straight time" for the first 40 hours worked by each employee and pays
7 "time-and-a-half" for all hours worked in excess of 40 hours. You are given a
8 list of the employees of the company, the number of hours each employee worked
9 last week and the hourly rate of each employee. Your program should input this
10 information for each employee and should determine and display the employee's
11 gross pay.
13 Enter hours worked (-1 to end): 39
14 Enter hourly rate of the employee ($00.00): 10.00
15 Salary is $390.00
17 Enter hours worked (-1 to end): 40
18 Enter hourly rate of the employee ($00.00): 10.00
19 Salary is $400.00
21 Enter hours worked (-1 to end): 41
22 Enter hourly rate of the employee ($00.00): 10.00
23 Salary is $415.00
25 Enter hours worked (-1 to end): -1
27 Written by Juan Carlos Moreno (jcmhsoftware@gmail.com), 2022-12-22.*/
29 #include <iostream>
30 #include <iomanip>
32 using namespace std;
34 int main()
36 int hours; //Number of hours worked
37 double rate; //Hourly rate of the worker
38 double salary; //Employee's gross pay
40 while (hours != -1)
42 cout << "Enter # of hours worked (-1 to end): ";
43 cin >> hours;
45 if (hours != -1)
47 cout << "Enter hourly rate of the employee ($00.00): ";
48 cin >> rate;
50 //The company pays "straight time"
51 if (hours <= 40)
53 salary = hours*rate;
55 //The company pays "time-and-a-half"
56 else if (hours > 40)
58 salary = 40*rate + 1.5*(hours - 40)*rate;
60 cout << setprecision(2) << fixed;
61 cout << "Salary is $" << salary << endl << endl;
65 return 0;