ok i have correct and update my code and it does work with no errors expect it doesnt do calculations and bubblesort with employees last name
// here is the txt file//
Code:
40.0 10.00 A1234 Jane Adams
50.0 10.00 L8765 Mary Lincoln
25.5 10.85 W7654 Martha Washington
52.0 15.75 A9876 John Adams
45.0 25.00 W1235 George Washington
40.25 55.00 L9087 Abraham Lincoln
30.0 9.75 T9876 William Tell
42.5 12.50 M7654 Missy Muffett
30.0 10.00 P8765 Peter Piper
//heres my code//
Code:
#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
//results i get after compiling the program *** CALCULATIONS AND BUBVLE SORT (arrange from from employees last name***//
Code:
First Last Employee Hours Rate Regular Overtime Gross
Name Name Number Worked of Pay Pay Pay Pay
======================================================
Jane Adams A1234 40.00 10.00 1 1 1
Mary Lincoln L8765 50.00 10.00 1 1 1
Martha Washington W7654 25.50 10.85 1 1 1
John Adams A9876 52.00 15.75 1 1 1
George Washington W1235 45.00 25.00 1 1 1
Abraham Lincoln L9087 40.25 55.00 1 1 1
William Tell T9876 30.00 9.75 1 1 1
Missy Muffett M7654 42.50 12.50 1 1 1
Peter Piper P8765 30.00 10.00 1 1 1
===================================================
Total Gross Pay 1