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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
// Pre-processors.
#include<cctype>
#include<cstring>
#include<float.h>
#include<fstream>
#include<iomanip>
#include<iostream>
using namespace std;
// Constants.
const int MAX = 100; // Arbitrary maximum.
const int NAME = 16; // Available size for Emp.'s name.
// Prototypes.
void welcome_user(); // Welcome the user to the program.
void cls(); // Clears the screen of text.
void add_user(); // Get the user's info.
void calculate(char[], float, float, float, float, float); // Calculate the Emp.'s pay.
bool writeout(char [], float, float, float, float, float); // Write out Employee data.
// Function Implementations.
void cls() // Clear the screen of text.
{
for(int i = 0; i < 30; i++)
cout << "|" << endl;
}
void welcome_user() // Welcome user to the program.
{ cout << "| welcome to the program!" << endl << endl << endl; }
int main()
{
welcome_user();
int num_to_enter;
cout << " How many employees would you like to input? : ";
cin >> num_to_enter;
cin.ignore(MAX, '\n');
for(int i = 0; i < num_to_enter; i++)
{
cls();
add_user();
}
}
// Get employee information.
void add_user()
{
char uname[NAME]; // Employee's name.
float ehours, ewage, overtime = 0.00; // Employee data.
float etax_rate, retirement; // Withholdings.
cout << "| > Enter the employee's name: ";
cin.get(uname, NAME, '\n');
cin.ignore(NAME, '\n');
cls();
cout << "| > Enter " << uname << "'s hourly wage: $ ";
cin >> ewage;
cin.ignore(MAX, '\n');
cls();
cout << "| > Number of hours worked: ";
cin >> ehours;
cin.ignore(MAX, '\n');
cls();
cout << "| What is the Income Tax Rate? (as a %) " << endl
<< "| Ex: 2 = 2.00% (0.02), 0.25 = 0.25% (0.0025)" << endl
<< "| > : % ";
cin >> etax_rate;
cin.ignore(MAX, '\n');
etax_rate = (etax_rate / 100); // Convert percentage to decimal.
cls();
cout << "| How much should be withheld for retirement? (USD)" << endl
<< "| Ex: $ 1, $ 15.00, $ 1305.90 " << endl
<< "| > : $ ";
cin >> retirement;
cin.ignore(MAX, '\n');
calculate(uname, ehours, ewage, etax_rate, retirement, overtime); // Calculate & write.
}
// Run calculations for this iteration (employee).
void calculate(char empname[], float hours, float wage, float tax_rate, float retire, float ot)
{
cout << " Calculating..." << endl << endl << endl;
float total_wage, wage_aftholds; // Wage before & after withholdings.
float ot_wage = 0.00, taxes_held;
if(hours > 40) // If the employee earned OT...
{
int selection;
do
{
cout << "| >> Overtime detected << " << endl
<< "| How much is overtime? " << endl
<< "|------------------------ " << endl
<< "| 1) Time & 1/4 " << endl
<< "| 2) Time & 1/2 " << endl
<< "| 3) Double Time " << endl
<< "|------------------------ " << endl
<< "| : ";
cin >> selection;
cin.ignore(MAX, '\n');
switch(selection)
{
case 1:
ot_wage = wage * 1.25; // Set time & a quarter.
break;
case 2:
ot_wage = wage * 1.50; // Set time & a half.
break;
case 3:
ot_wage = wage * 2.00; // Set double-time.
break;
default: // Handle invalid input.
cls();
cout << " Invalid selection. Please try again." << endl << endl << endl;
}
}while(!(selection > 0 && selection < 4));
ot = (hours - 40) * ot_wage; // Calculate the overtime earned ($).
total_wage = (wage * 40) + ot; // Calculate pay including OT.
}
else // If not...
{
ot = 0.00;
total_wage = (hours * wage); // Calculate the pay earned.
}
// Calculate withholdings.
wage_aftholds = total_wage - ((total_wage * tax_rate) + retire);
// Calculate taxes($) withheld.
taxes_held = total_wage * tax_rate;
cout << endl << " Calculations finished! " << endl << endl << endl;
cls();
char choice; // Output zee results!
cout << "|====================================== " << endl
<< "| Employee: " << empname << endl
<< "|------------------------- " << endl
<< "| Hourly Wage: $ " << wage << " / hr " << endl
<< "|------------------------- " << endl
<< "| Tax Rate: " << tax_rate * 100 << " %" << endl
<< "|====================================== " << endl
<< "| This pay period " << endl
<< "|- - - - - - - - - - - - - - - - - - - " << endl
<< "| " << endl
<< "| > Pay (pre-tax): $ " << total_wage << endl
<< "| " << endl
<< "| > Pay earned: $ " << wage_aftholds << endl
<< "| " << endl
<< "|- - - - - - - - - - - - - - - - - - - " << endl
<< "| > Hours worked: " << hours << endl
<< "| > Overtime earned: $ " << ot << endl
<< "| OT ratio (%): $ " << ot_wage << endl
<< "| " << endl
<< "| Withholdings: " << endl
<< "| > Retirement: -$ " << retire << endl
<< "| > Taxes: -$ " << taxes_held << endl
<< "|====================================== " << endl
<< "| " << endl
<< "| >>> Save to file? (y/n): ";
cin >> choice;
cin.ignore(MAX, '\n');
choice = tolower(choice);
if(choice == 'y')
{
bool handler = NULL;
do // Loops in case the file couldn't be connected to.
{
// Write out the data.
handler = writeout(empname, hours, wage, retire, taxes_held, ot);
}while(!handler);
cls();
cout << "| >>> Data successfully saved! " << endl << endl << endl;
}
else
cout << " Confirmed. Returning..." << endl << endl << endl;
}
|