Code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MIN_HOURS 0.00 // minimum hours per week
#define MAX_HOURS 60.00 // maximum hours per week
#define MIN_RATE 0.00 // minimum pay rate
#define MAX_RATE 99.99 // maximum pay rate
#define REGULAR_HOURS_LIMIT 40.00 // hours at which overtime begins
#define OVERTIME_RATE 1.50 // overtime rate
#define TAX_RATE 0.30 // tax rate (30%)
#define PARKING_RATE 10.00 // parking deduction
#define TRANSIT_RATE 5.00 // transit deduction
void input_employee_data(string &full_name, double &hours,
double &hourly_rate, double &deductions);
string join_name(string first_name, string last_name);
double input_bounded_value(string description, double min_value,
double max_value);
double calculate_deductions();
void split_hours(double hours, double ®ular_hours,
double &overtime_hours);
double calculate_gross_pay(double regular_hours, double overtime_hours,
double hourly_rate);
double calculate_tax(double gross_pay, double tax_rate);
double calculate_net_pay(double gross_pay, double tax,
double deductions);
void output_payroll_data(string full_name, double regular_hours,
double overtime_hours, double hourly_rate, double gross_pay,
double tax, double deductions, double net_pay);
char get_yesno(string prompt);
int main()
{
char array_names[10];
int num_emps; // number of employees processed
string full_name; // employee's full name
double hours, // number of hours worked
regular_hours, // number of regular hours
overtime_hours, // number of overtime hours
hourly_rate, // hourly pay rate
gross_pay, // employee's gross pay
tax, // employee's tax amount
deductions, // after-tax deductions
net_pay, // employee's net pay
total_gross_pay; // total gross pay of employees
cout << fixed << setprecision(2);
// Initialize counters and accumulators
num_emps = 0;
total_gross_pay = 0.00;
do {
#define SIZE 10
// Input section
input_employee_data(full_name, hours, hourly_rate, deductions);
// Processing section
split_hours(hours, regular_hours, overtime_hours);
gross_pay = calculate_gross_pay(regular_hours, overtime_hours,
hourly_rate);
tax = calculate_tax(gross_pay, TAX_RATE);
net_pay = calculate_net_pay(gross_pay, tax, deductions);
// Output section
output_payroll_data(full_name, regular_hours, overtime_hours,
hourly_rate, gross_pay, tax, deductions,
net_pay);
// Increment counters and accumulators
num_emps = num_emps + 1;
total_gross_pay = total_gross_pay + gross_pay;
} while (get_yesno("Process another employee") == 'Y');
// Output summary information
cout << endl;
cout << "Employees processed: " << num_emps << endl;
cout << "Total gross pay: $" << total_gross_pay << endl;
}
//
// Reads the employee's name, hours and hourly pay rate from the user
// and calculates deductions based on the employee's use of the parking
// garage or participation in the transit program.
//
void input_employee_data(string &full_name, double &hours,
double &hourly_rate, double &deductions)
{
string first_name, // employee's first name, input by user
last_name; // employee's last name, input by user
cout << endl;
cout << "Enter employee's first name: ";
cin >> first_name;
cout << "Enter employee's last name: ";
cin >> last_name;
full_name = join_name(first_name, last_name);
hours = input_bounded_value("number of hours worked", MIN_HOURS,
MAX_HOURS);
hourly_rate = input_bounded_value("hourly pay rate", MIN_RATE,
MAX_RATE);
deductions = calculate_deductions();
}
//
// Concatenates an employee's first and last names to a full name in the
// form "Last, First".
//
string join_name(string first_name, string last_name)
{
string full_name; // employee's concatenated full name
full_name = last_name + ", " + first_name;
return full_name;
// or just the following:
// return last_name + ", " + first_name;
}
// Reads a value from the user and validates that it is within a
// specific range. The user is prompted to enter a numeric value
// as described by the 'description' parameter (e.g., "number of
// hours worked"). Once the number entered by the user is between
// 'min_value' and 'max_value', it is returned via the 'value'
// parameter.
//
double input_bounded_value(string description, double min_value,
double max_value)
{
double value; // value read from the user
do {
cout << "Enter " << description << ": ";
cin >> value;
if (value < min_value || value > max_value) {
cout << "The " << description << " must be between "
<< min_value << " and " << max_value << endl
<< endl;
}
} while (value < min_value || value > max_value);
return value;
}
//
// Asks the user various yes/no questions to determine the amount of an
// employee's after-tax deductions.
//
double calculate_deductions()
{
double deductions; // after-tax deduction amount
if (get_yesno("Does the employee use the parking garage") == 'Y') {
deductions = PARKING_RATE;
}
else if (get_yesno("Does the employee participate in the "
"transit program") == 'Y') {
deductions = TRANSIT_RATE;
}
else {
deductions = 0;
}
return deductions;
}
//
// Splits the total number of hours worked by an employee into regular
// and overtime hours.
//
void split_hours(double hours, double ®ular_hours,
double &overtime_hours)
{
if (hours <= REGULAR_HOURS_LIMIT) {
regular_hours = hours;
overtime_hours = 0.0;
}
else {
regular_hours = REGULAR_HOURS_LIMIT;
overtime_hours = hours - REGULAR_HOURS_LIMIT;
}
}
//
// Calculates an employee's gross pay, given the number of hours worked
// (regular and overtime) and hourly rate.
//
double calculate_gross_pay(double regular_hours, double overtime_hours,
double hourly_rate)
{
double gross_pay; // employee's gross pay
gross_pay = (regular_hours * hourly_rate) + (overtime_hours *
hourly_rate * OVERTIME_RATE);
return gross_pay;
// or just the following:
// return (regular_hours * hourly_rate) + (overtime_hours *
// hourly_rate * OVERTIME_RATE);
}
//
// Calculates the tax on an employee's gross pay.
//
double calculate_tax(double gross_pay, double tax_rate)
{
double tax; // employee's tax amount
tax = gross_pay * tax_rate;
return tax;
// or just the following:
// return gross_pay * tax_rate;
}
//
// Calculates an employee's net pay, given gross pay, taxes and
// deductions.
//
double calculate_net_pay(double gross_pay, double tax,
double deductions)
{
double net_pay; // employee's net pay
net_pay = gross_pay - tax - deductions;
return net_pay;
// or just the following:
// return gross_pay - tax - deductions;
}
//
// Displays formatted payroll data for an employee, including headers.
//
void output_payroll_data(string full_name, double regular_hours,
double overtime_hours, double hourly_rate, double gross_pay,
double tax, double deductions, double net_pay)
{
cout << endl;
cout << " Reg. Ovt. Hourly Gross "
<< " Net " << endl;
cout << "Name Hours Hours Rate Pay "
<< " Taxes Deduct Pay " << endl;
cout << "==================== ===== ===== ====== ======="
<< " ====== ====== =======" << endl;
cout << left << setw(20) << full_name << " ";
cout << right;
cout << setw(5) << regular_hours << " ";
cout << setw(5) << overtime_hours << " ";
cout << setw(6) << hourly_rate << " ";
cout << setw(7) << gross_pay << " ";
cout << setw(6) << tax << " ";
cout << setw(6) << deductions << " ";
cout << setw(7) << net_pay << endl << endl;
}
//
// Prompts the user to answer a yes/no question, providing a validated
// 'Y' or 'N' (only) via the out parameter.
//
char get_yesno(string prompt)
{
char yesno; // user's yes or no response
do {
cout << prompt << " (Y/N)? ";
cin >> yesno;
if (yesno == 'y') {
yesno = 'Y';
}
else if (yesno == 'n') {
yesno = 'N';
}
if (yesno != 'Y' && yesno != 'N') {
cout << "Please type 'Y' for yes or 'N' for no"
<< endl << endl;
}
} while (yesno != 'Y' && yesno != 'N');
return yesno;
}