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
|
#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 []);
void bubbleSort( string [], string [], string [], double [], double [], int);
void findregularhours(double[], int[], int);
void findregularpay(int[], double[], double[], int);
void findovertimehours(double[], double[], int);
void findovertimepay(double[], double[], double[], int);
void findgrosspay(double[], double[], double[], int);
int outputScr(double[], double[], int[]);
int main()
{
string firstname[SZ], lastname[SZ];
string empids[SZ];
double hoursworked[SZ], overtimehours[SZ];
int regularhours[SZ];
double hourlyrate[SZ], regularpay[SZ], overtimepay[SZ], grosspay[SZ];
int n, numemp;
ofstream outputFile;
bool swapmade = false;
bool screenonly = false;
tellUser();
//readData(firstn, lastn, empID, hrs, rate);
bubbleSort(firstname, lastname, empids, hoursworked, hourlyrate, numemp);
findregularhours(hoursworked, regularhours, n);
findovertimehours(hoursworked, overtimehours, n);
findovertimepay(overtimehours, hourlyrate, overtimepay, n);
findgrosspay(regularpay, overtimepay, grosspay, n);
outputScr(hoursworked, hourlyrate, regularhours);
return 0;
} //end main
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 and output is written to the screen. \n\n";
} //end tellUser Function
int readData(string firstn[], string lastn[], string empID[], double hrs[], double rate[])
{
int numemp;
ifstream inputFile;
int i = 0;
// open file and read inputs from employees.txt
inputFile.open("employees2.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();
}
}
void bubbleSort(string firstn[ ],string lastn[ ],string empID[ ],double hrs[ ],double rate[ ],int numemp)
{
//string firstn[SZ], last[SZ], empID[SZ];
//double hrs[SZ], rate[SZ];
int regularhours[SZ];
int i, lastpos;
bool screenonly = false;
bool swapmade = false;
ofstream outputFile;
lastpos = numemp;
{
lastpos--;
swapmade = false;
for ( i = 0; i < lastpos; i++)
{
if (lastn[i] > lastn[i+1])
{ // swap all items here
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]);
//swap(findregularpay[i], findregularpay[i+1]);
//swap(findovertimepay[i], findovertimepay[i+1]);
//swap(findgrosspay[i], findgrosspay[i+1]);
swapmade = true;
}
}
} while(swapmade);
} //end of bubbleSort Function
void findregularhours(double hoursworked[], int regularhours[], int n)
{
for(int i=0; i<n; i++)
{
if(hoursworked[i]>40)
regularhours[i]=40;
else
regularhours[i]=hoursworked[i];
} //end for loop
} //regularhours()
void findregularpay( int regularhours[], double regularpay [], double hourlyrate[], int n)
{
for(int i=0; i<n; i++)
{
regularpay[i] = regularhours[i]*hourlyrate[i];
} //end for loop
} //regularpay
void findovertimehours(double hoursworked[], double overtimehours[], int n)
{
for(int i=0; i<n; i++)
{
if(hoursworked[i]>40)
overtimehours[i]=hoursworked[i]-40;
else
overtimehours[i]=0;
} //end for loop
}
void findovertimepay(double overtimehours[], double hourlyrate[], double overtimepay[], int n)
{
for(int i=0; i<n; i++)
{
overtimepay[i]=overtimehours[i]*hourlyrate[i]*1.5;
}
}
void findgrosspay(double regularpay[], double overtimepay[], double grosspay[], int n)
{
for(int i=0; i<n; i++)
{
grosspay[i]=regularpay[i]+overtimepay[i];
}
}
int outputScr(double hoursworked[], double hourlyrate[], int regularhours[])
{
string firstn[SZ], lastn[SZ];
string empID[SZ];
double hrs[SZ];
double rate[SZ];
double grosspay[SZ], regularpay[SZ], overtimepay[SZ];
int i, numemp, lastpos;
bool screenonly = false;
ofstream outputFile;
cout << "Payroll being written to file payroll.txt\n\n";
outputFile.open("payroll.txt"); // output file
numemp = readData(firstn, lastn, empID, hrs, rate);
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";
outputFile <<"First Last Employee Hours Rate Regular Overtime Gross\n";
outputFile <<"Name Name Number Worked of Pay Pay Pay Pay\n";
for (i = 0; i < numemp; i++) //screen only
{
cout << setw(15) << fixed << left << firstn[i] << setw(12) << lastn[i];
cout << setw(11) << empID[i] << " " << setw(12) << fixed << setprecision(2) << hrs[i] << " ";
cout << setw(10) << rate[i] << " " << setw(10) << fixed << setprecision(2) << regularpay[i] << " ";
cout << setw(11) << overtimepay[i] << " " << setw(12) << fixed << setprecision(2) << grosspay[i] << " " << endl;
if (!screenonly)
{
outputFile << setw(13) << fixed << left << firstn[i] << " ";
outputFile << setw(9) << fixed << lastn[i] << " ";
outputFile << setw(9) << fixed << right << empID[i] << " ";
outputFile << setw(9) << fixed << setprecision(2) << hrs[i] << " ";
outputFile << setw(12) << fixed << setprecision(2) << rate[i] << " ";
outputFile << setw(10) << fixed << setprecision(2) << regularpay[i] << " ";
outputFile << setw(11) << fixed << setprecision(2) << overtimepay[i] << " ";
outputFile << setw(10) << fixed << setprecision(2) << grosspay[i] << " " << endl;
}
}
cout <<"\t\t\tTotal Gross Pay\t\t\t\t\t "<< fixed << setprecision(2) << findgrosspay << " \n";
outputFile <<"\t\t\tTotal Gross Pay\t\t\t\t\t "<< fixed << setprecision(2) << findgrosspay << " \n";
if (!screenonly)
{
outputFile.close();
cout << "input file closed\n\n";}
}
|