1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#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
void show_welcome_message();
void input_employee_data(string &first_name, string &last_name, string &hours, string &hourly_rate);
void join_name(string &first_name,string &last_name,string full_name);
void split_hours(double &hours,double regular_hours,double overtime_hours);
void calculate_gross_pay(double ®ular_hours,double &overtime_hours,double &hourly_rate,double gross_pay);
void calculate_tax(double &gross_pay, double &tax_rate, double tax);
void calculate_net_pay(double &gross_pay,double &tax,double &deduction,double net_pay);
void output_payroll_data(string &full_name,double ®ular_hours,double &overtime_hours,double &hourly_rate,double &gross_pay,double &tax,double &deductions,double &net_pay);
void get_yesno(string &prompt,char yesno);
int main()
{
string first_name, // employee's first name, input by user
last_name, // employee's last name, input by user
full_name; // employee's concatenated full name
double hours, // number of hours worked, input by user
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, // monthly deductions
net_pay; // employee's net pay
char yesno; // user's yes or no response
show_welcome_message();
input_employee_data(first_name,last_name,hours,hourly_rate);
join_name(first_name,last_name,full_name);
split_hours(hours,regular_hours,overtime_hours);
calculate_gross_pay(regular_hours,overtime_hours,hourly_rate,gross_pay);
calculate_tax(gross_pay,tax_rate,tax);
calculate_net_pay(gross_pay,tax,deduction,net_pay);
output_payroll_data(full_name,regular_hours,overtime_hours,hourly_rate,gross_pay,tax,deductions,net_pay);
get_yesno(prompt,yesno);
}
//
// Displays a welcome message to the user, with the name of the program
// and a short discription.
//
void show_welcome_message()
{
cout << "Payroll Program" << endl << endl;
cout << "This program computes and displays the gross pay, taxes,"
<< endl;
cout << "and net pay for employees of the Super Supermarket, given"
<< endl;
cout << "each employee's name, number of hours worked, and hourly"
<< endl;
cout << "pay rate."
<< endl;
}
//
// Prompts user to enter employee first name and last name
//
void input_employee_data(string &first_name, string &last_name, string &hours, string &hourly_rate)
{
do {
cout << "Enter employee's first name: ";
cin >> first_name;
cout << "Enter employee's last name: ";
cin >> last_name;
do {
cout << "Enter number of hours worked: ";
cin >> hours;
if (hours < MIN_HOURS || hours > MAX_HOURS)
cout << fixed << setprecision(2)
<< "The number of hours worked must be between "
<< MIN_HOURS << " and " << MAX_HOURS << endl
<< endl;
} while (hours < MIN_HOURS || hours > MAX_HOURS);
do {
cout << "Enter hourly pay rate: ";
cin >> hourly_rate;
if (hourly_rate < MIN_RATE || hourly_rate > MAX_RATE)
cout << fixed << setprecision(2)
<< "The hourly pay rate must be between "
<< MIN_RATE << " and " << MAX_RATE << endl
<< endl;
} while
(hourly_rate < MIN_RATE || hourly_rate > MAX_RATE);
}
}
//
// Began processing names
//
void join_name(string &first_name, string &lastname, string fullname)
{
full_name = last_name + ", " + first_name;
}
//
// Began processing split hours
//
void split_hours(double &hours,double regular_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;
}
}
//
// Began calculating gross pay
//
void calculate_gross_pay(double ®ular_hours, double &overtime_hours, double &hourly_rate, double gross_pay)
{
gross_pay = (regular_hours * hourly_rate) + (overtime_hours *
hourly_rate * OVERTIME_RATE);
}
//
// Began calculating tax
//
void calculate_tax(double &gross_pay,double &tax_rate,double tax)
{
tax = gross_pay * TAX_RATE;}
//
// Deduction
//
deductions = PARKING_RATE;
//
// Began calculating net pay
//
void calculate_net_pay(double &gross_pay,double &tax,double &deduction,double net_pay)
{
net_pay = gross_pay - tax - deductions;}
//
// Began outputting payroll data
//
void output_payroll_data(string &full_name,double ®ular_hours,double &overtime_hours,double &hourly_rate,double &gross_pay,double &tax,double &deductions,double &net_pay)
{
cout << endl;
cout << "12345678901234567890##21.00##21.00##321.00##4321.00##321.00##321.00##4321.00" << 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 << fixed << setprecision(2);
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, returning 'Y' or 'N'.
//
void get_yesno(string &prompt,char yesno)
{
do {
cout << "Process another employee (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');
cout << endl;
} while (yesno == 'Y');cout << endl;
|