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
|
#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();
int regular();
int overtime();
int 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;
numemp = readData(firstname, lastname, empids, hours, rates);
for (i = 0; i < numemp; i++)
{
cout << firstname[i] << lastname[i] << empids[i] << hours[i]
<< rates[i] << endl;
}
bubbleSort();
regular();
overtime();
grossPay();
outputScr();
return 0;
} //end main
/* rellUser(0
* tells about the program to the user
*/
void tellUser()
{
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.\n";
cout <<"output is written to the screen. \n\n"; //tell user what program does
}
/**************************************************
* readData
* firstname , lastname, empID, hours, rate of pay
*/
int readData(string firstn[], string lastn[], string empID[], double hrs[], double rate[])
{
int numemp;
ifstream inputFile;
int i = 0;
tellUser();
// open file and read inputs from employees.txt
inputFile.open("employees.txt");
if (inputFile.fail())
{
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 ****************//
cout << "input file closed\n\n";
}
}
/* bubbleSort()
* sorts employees with theor last name snd displays in
* screen and txt file
*/
int bubbleSort()
{
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;
cout << "sorting golfer\n";
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);
}
/* regular()
* calculates employees regular pay
*/
int regular()
{
double hours[i];
double rate;
if(hours <= 40)
grossPay = hours * rate;
}
/* overtime()
* calculates employees overtime pay
*/
int overtime()
{
double hours, rate;
// const int emp_hrs = 40;
// const double emp_over = 1.5;
if (hours >= 40)
overtime = (hours - 40) * rate * 1.5;
}
/* grossPay()
* calculates employees regular + overtime pay
*/
int grossPay()
{
double hours, rate;
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 firstn[SZ], lastn[SZ];
string empID[SZ];
double hrs[SZ];
double rate[SZ];
int i, numemp, lastpos;
bool screenonly = false;
ofstream outputFile;
cout << "Payroll being written to file payroll.txt\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";
for ( i=0; i < numemp; i++)
{
cout<< setw(7) << firstn[i] << setw(13) << lastn[i];
cout<< setw(12) << empID[i] << " " << setw(10) << hrs[i] << " ";
cout<< setw(11) << rate[i] << " " << setw(11) << regular << " ";
cout<< setw(11) << overtime << " " << setw(11) << grossPay << " \n";
if (!screenonly)
{
outputFile << setw(7) << left << firstn[i] << " ";
outputFile << setw(7) << left << lastn[i] << " ";
outputFile << setw(4) << fixed << right << empID << " ";
outputFile << setw(4) << fixed << right << hrs[i] << " ";
outputFile << setw(4) << fixed << right << rate[i] << " \n";
}
}
if (!screenonly)
{
outputFile.close(); cout << "Output file closed\n\n";}
}
|