Loops 4

Description

The purpose of this challenge is to use loops for input validation. This challenge creates a mock payroll stub.

Requirements

  1. Ask the user to enter a non-integer value. This value will represent the number of regular hours worked in a week. Use a while loop to implement input validation to reject values < 0. Additionally, the user should not be able to enter values greater than 40.
  2. Ask the user to enter a non-integer value. This value will represent the number of overtime hours worked. Similarly, only allow positive numbers less than or equal to 20. If the user entered less than 40 regular hours, DO NOT ask for overtime hours and set overtime hours to zero. Use a do-while loop for this input validation. 
  3. Ask the user to enter a non-integer value. This value will represent the user’s hourly pay rate. Similarly, only allow positive numbers to be entered. Choose either a while or do-while for this validation.
  4. Calculate the base income by calculating regular hours x rate
  5. Calculate overtime income by calculating overtime hours * rate * 1.5
  6. Calculate total gross income as the sum of base income plus overtime income
  7. Calculate tax as follows: If the weekly gross income exceeds $2000.00, calculate tax to be 25% of gross income. Otherwise, calculate tax to be 15% of gross income.
  8. Calculate net pay as gross income less taxes
  9. Display an abbreviated payroll stub as shown in the output below

Sample Interaction / Output

How many regular hours did you work this week? -5
Hours entered cannot be accepted
How many regular hours did you work this week? 50
Hours entered cannot be accepted
How many regular hours did you work this week? 35

What is your hourly rate? -10
Invalid input
What is your hourly rate? 20.50

PAYROLL INFO
------------
Hours worked: 35
Hourly rate: $20.50
Overtime hours: 0
Overtime pay: 0
Gross pay: $717.50
Tax: $107.63
Net pay: $609.87

RUN IT AGAIN

How many regular hours did you work this week? -5
Hours entered cannot be accepted
How many regular hours did you work this week? 50
Hours entered cannot be accepted
How many regular hours did you work this week? 40

How many O/T hours did you work? 30
You may not enter hours that exceed 20
How many O/T hours did you work? 10

What is your hourly rate? -10
Invalid input
What is your hourly rate? 50.50

PAYROLL INFO
------------
Hours worked: 40
Hourly rate: $50.50
Overtime hours: 10
Overtime pay: $757.50
Gross pay: $2777.50
Tax: $694.38
Net pay: $2083.12

LEGEND
PROGRAM OUTPUT
USER ENTRY
FROM INPUT

CATALOG ID: CPP-LOOP0004

Print Requirements