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
|
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int SZ = 55;
void tellUser();
int readData( string [], string [], string [], double [], double []);
int bubbleSort();
int outputScr();
void regular();
void overtime();
void grossPay();
int main()
{
string firstname[SZ], lastname[SZ];
string empids[SZ];
double hours[SZ];
double rates[SZ];
ofstream outputFile;
bool swapmade = false;
bool screenonly = false;
char yesno;
int i, numemp, lastpos;
tellUser(); // tells the user about the program
bubbleSort(); // sorts the employees from their last , first name
outputScr(); // display employee payroll information to screen and txt file
return 0;
} //end main
/* tellUser()
* tells about the program to the user
*/
void tellUser() // tellUser Function
{
cout <<"\nThis program reads a file called employees.txt,\n";
cout <<"and it calculates the regular pay, overtime pay\n";
cout <<"and grosspay and total for grosspay for each employee and\n";
cout <<"sorts the from last name and output is written to the screen. \n\n"; //tell user what program does
} //end tellUser Function
/*
* readData
* firstname , lastname, empID, hours, rate of pay
*/
int readData(string firstn[], string lastn[], string empID[], double hrs[], double rate[]) //readData Function
{
int numemp;
ifstream inputFile;
int i = 0;
// tellUser();
// open file and read inputs from employees.txt
inputFile.open("employees.txt"); //open employees.txt file
if (inputFile.fail()) // employee.txt fails to open
{
cout << "Error opening file employees.txt \n\n";
cout << "end of program\n\n";
}
else
{
while ((inputFile >> hrs[i]) && (i < SZ))
{
inputFile >> rate[i];
inputFile >> empID[i];
inputFile >> firstn[i];
inputFile >> lastn[i];
i++;
} //end while
// cout << "There were " << i << " employess\n\n";
// numemp = i;
inputFile.close();
//************* close input file ****************//
}
} //en dof readData Function
/* bubbleSort()
* sorts employees with theor last name snd displays in
* screen and txt file
*/
int bubbleSort() //bubbleSort Function
{
string firstn[SZ], lastn[SZ];
string empID[SZ];
double hrs[SZ];
double rate[SZ];
int i, numemp, lastpos;
bool screenonly = false;
ofstream outputFile;
bool swapmade = false;
lastpos = numemp;
do
{
lastpos--;
swapmade = false;
for ( i = 0; i < lastpos; i++)
{
swap(firstn[i], firstn[i+1]);
swap(lastn[i], lastn[i+1]);
// swap(empID[i], empID[i+1]);
// swap(hrs[i], hrs[i+1]);
// swap(rate[i], rate[i+1]);
swapmade - true;
}
} while(swapmade);
} //end of bubbleSort Function
/* regular()
* calculates employees regular pay
*/
void regular()
{
double grossPay;
double hours, rates;
if(hours <= 40)
grossPay = hours * rates;
}
/* overtime()
* calculates employees overtime pay
*/
void overtime()
{
double hours, rate, overtime;
if (hours >= 40)
overtime = (hours - 40) * rate * 1.5;
}
/* grossPay()
* calculates employees regular + overtime pay
*/
void grossPay()
{
double hours, rate, grossPay;
if (hours <= 40)
grossPay = (hours * rate);
else
grossPay = ((hours - 40) * rate * 1.5);
}
/* outputScr()
* displays the employee payroll function to the screen and txt file
*/
int outputScr()
{
string firstname[SZ], lastname[SZ];
string empids[SZ];
double hours[SZ];
double rates[SZ];
int i, numemp, lastpos;
bool screenonly = false;
ofstream outputFile;
cout << "Payroll being written to file payroll.txt\n\n"; //output function
outputFile.open("payroll.txt"); // output file
if (outputFile.fail())
{
screenonly = true;
cout <<" output file did not open\n";
cout <<" output file will only be sent to the screen\n";
}
cout <<" First Last Employee Hours Rate Regular Overtime Gross\n";
cout <<" Name Name Number Worked of Pay Pay Pay Pay\n";
cout <<"============================================================================================\n";
numemp = readData(firstname, lastname, empids, hours, rates);
for (i = 0; i < numemp; i++)
{
cout << setw(7) << firstname[i] << setw(12) << lastname[i];
cout << setw(11) << empids[i] << " " << setw(12) << fixed << setprecision(2) << hours[i] << " ";
cout << setw(11) << rates[i] << " " << setw(7) << fixed << setprecision(2) << regular << " ";
cout << setw(9) << overtime << " " << setw(12) << fixed << setprecision(2) << grossPay << " " << endl;
if (!screenonly)
{
outputFile << setw(7) << left << firstname[i] << " ";
outputFile << setw(12) << left << lastname[i] << " ";
outputFile << setw(11) << fixed << right << empids << " ";
outputFile << setw(12) << fixed << right << hours[i] << " ";
outputFile << setw(11) << fixed << right << rates[i] << " \n";
}
}
cout <<"============================================================================================\n";
cout <<"\t\t\tTotal Gross Pay \t\t\t\t\t" << fixed << setprecision(2) << grossPay << " \n";
if (!screenonly)
{
outputFile.close();
cout << "inpur file closed\n\n";}
} // end main
|