|
Example 4-14 |
The following program
determines an employee’s weekly wages. If the hours worked exceed 40, wages
include overtime payment.
//Program:
Weekly wages
#include <iostream>
#include <iomanip>
using namespace std;
int
main()
{
double
wages, rate, hours;
cout<<fixed<<showpoint<<setprecision(2); //Line 1
cout<<"Line
2: Enter working hours and rate: "; //Line 2
cin>>hours>>rate; //Line 3
if(hours
> 40.0)
//Line 4
wages = 40.0 *
rate +
1.5 * rate * (hours - 40.0); //Line 5
else
//Line 6
wages = hours *
rate; //Line 7
cout<<endl; //Line 8
cout<<"Line
9: The wages are $"<< wages<<endl; //Line 9
return
0;
}
Sample Run In this sample run, the user input is shaded.
Line 2: Enter working hours and rate: 56.45 12.50
Line
9: The wages are $808.44
The statement at Line 1 sets
the output of the floating-point numbers in a fixed decimal format, with a
decimal point, trailing zeros, and two decimal places. The statement at Line 2
prompts the user to input the number of hours worked and the pay rate. The
statement at Line 3 inputs these values into the variables hours and rate,
respectively. The statement at Line 4 checks whether the value of the variable hours is greater than 40.0. If hours is
greater than 40.0, then the
wages are calculated by the statement at Line 5, which includes overtime
payment. Otherwise, the wages are calculated by the statement at Line 7. The
statement at Line 9 outputs the wages.