Instructions
Requirements and Specifications
Source Code
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
/*
GET DATA MODULE
*/
void GetData(ifstream & file, string& initials, float &h1, float &h2, float &h3, float &h4, float &h5, float &h6, float &h7, float &rate, bool &good)
{
//m Get data
file >> initials >> h1 >> h2 >> h3 >> h4 >> h5 >> h6 >> h7 >> rate;
good = true;
// Open error file
ofstream out_file("Errors.txt", std::ios_base::app);
// Get errors and if there is any error, save to a error file
if(h1 < 0 || h1 > 80) {
out_file << "Error for worker " << initials << ", Hours for day 1 (" << h1 << ") is outside bounds [0, 80]" << endl;
good = false;
}
if(h2 < 0 || h2 > 80) {
out_file << "Error for worker " << initials << ", Hours for day 2 (" << h2 << ") is outside bounds [0, 80]" << endl;
good = false;
}
if(h3 < 0 || h3 > 80) {
out_file << "Error for worker " << initials << ", Hours for day 3 (" << h3 << ") is outside bounds [0, 80]" << endl;
good = false;
}
if(h4 < 0 || h4 > 80) {
out_file << "Error for worker " << initials << ", Hours for day 4 (" << h4 << ") is outside bounds [0, 80]" << endl;
good = false;
}
if(h5 < 0 || h5 > 80) {
out_file << "Error for worker " << initials << ", Hours for day 5 (" << h5 << ") is outside bounds [0, 80]" << endl;
good = false;
}
if(h6 < 0 || h6 > 80) {
out_file << "Error for worker " << initials << ", Hours for day 6 (" << h6 << ") is outside bounds [0, 80]" << endl;
good = false;
}
if(h7 < 0 || h7 > 80) {
out_file << "Error for worker " << initials << ", Hours for day 7 (" << h7 << ") is outside bounds [0, 80]" << endl;
good = false;
}
//Check rate
if(rate < 0) {
out_file << "Error for worker " << initials<< ", rate is negative." << endl;
good = false;
}
out_file.close();
}
/*
MODULE CALCULATE
*/
void Calculate(float h1, float h2, float h3, float h4, float h5, float h6, float h7, float rate, float &total_hours, float &gross_payment, float &federal_rate, float &state_rate, float &hospitalization, float &union_rate, float &net_payment)
{
total_hours = h1 + h2+ h3 + h4 + h5+h6 +h7;
if(total_hours > 40)
{
gross_payment = 40*rate; // regular pay
total_hours -= 40;
if(total_hours > 20)
{
total_hours -= 20;
gross_payment += 20*rate*1.5; // 50% more
// If there are still hours, pay them with double pay
if(total_hours > 0)
{
gross_payment += total_hours*rate*2.0; // double
}
}
else
{
gross_payment += total_hours*rate*1.5; // 50% more
}
}
else
gross_payment = total_hours*rate;
// Calculate deductions
federal_rate = gross_payment*0.18f;
state_rate = gross_payment*0.045f;
hospitalization = 25.65f;
union_rate = gross_payment*0.02f;
// Calculate net payment
net_payment = gross_payment - (federal_rate+state_rate+hospitalization+union_rate);
}
/*
MODULE SEND DATA
*/
void SendData(ofstream& file, string &initials, float rate, float total_hours, float gross_payment, float federal_rate, float state_rate, float hospitalization, float union_rate, float net_payment)
{
file << left << setw(20) << initials << right << setw(10) << rate << setw(20) << total_hours << setw(35) << federal_rate << setw(35) << state_rate << setw(25) << hospitalization << setw(25) << union_rate << setw(20) << gross_payment << setw(20) << net_payment << endl;
}
int main()
{
// Read file
ifstream file("PayIn.txt");
// Open out file
ofstream out_file("PayOut.txt");
out_file << left << setw(20) << "Initials" << right << setw(10) << "Hourly Rate" << setw(20) << "Total Hours" << setw(35) << "Federal Withholding Deduction" << setw(35) << "State Withholding Deduction" << setw(25) << "Hospitalization" << setw(25) << "Union dues" << setw(20) << "Gross Payment" << setw(20) << "Net Payment" << endl;
int n = 0;
float h1, h2, h3, h4, h5, h6, h7, rate;
float total_hours, gross_payment, net_payment, federal_rate, union_rate, state_rate, hospitalization;
//char *initials;
string initials;
bool good;
while(file.peek() != EOF)
{
GetData(file, initials, h1, h2, h3, h4, h5, h6, h7, rate, good);
if(good)
{
Calculate(h1, h2, h3, h4, h5, h6, h7, rate,total_hours, gross_payment,federal_rate,state_rate, hospitalization, union_rate, net_payment);
SendData(out_file, initials, rate, total_hours, gross_payment, federal_rate, state_rate, hospitalization, union_rate, net_payment);
}
}
out_file.close();
file.close();
}