Question & Answer: of employee data for how many hours each employee worked for a week. Then for eac…..

Write a C++ program that will need to read from a file of employee data for how many hours each employee worked for a week. Then for each of the employees, a total of the hours worked is made and then the employees are sorted into descending order based on their total hours using bubble sort.

There needs be 3 function calls total in main that:

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: of employee data for how many hours each employee worked for a week. Then for eac…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

Open and read the file into the array and add the total hours worked for each employee

Sort the array based on the total hours

Write out the output

Input file looks like this:

Jones,Frank 2 3 8 3635 Smith, Tiny 883082 Turk, Tom 9104700 e Jackson,Jim 5 6 56 5 6 5 Catan,Doug 7 387257

Output should look something similar to this:

Question & Answer: of employee data for how many hours each employee worked for a week. Then for eac..... 1

Jones,Frank 2 3 8 3635 Smith, Tiny 883082 Turk, Tom 9104700 e Jackson,Jim 5 6 56 5 6 5 Catan,Doug 7 387257

Expert Answer

 

C++ Program:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

//Structure definition
struct Employee
{
string name;
int sun, mon, tue, wed, thu, fri, sat, TT;
};

//Function that reads data from file
void readData(fstream &fin, Employee employees[], int numEmp)
{
//Temporary object
Employee temp;

//Reading data from file
for(int i=0; i<numEmp; i++)
{
//Reading a line of data
fin >> temp.name >> temp.sun >> temp.mon >> temp.tue >> temp.wed >> temp.thu >> temp.fri >> temp.sat;

//Adding all working hours
temp.TT = temp.mon + temp.tue + temp.wed + temp.thu + temp.fri + temp.sat + temp.sun;

//Adding to array
employees[i] = temp;
}
}

//Function that sorts data in descending order
void sortData(Employee employees[], int numEmp)
{
int i, j;

//Temporary object
Employee temp;

//Reading data from file
for(i=0; i<numEmp-1; i++)
{
//Iterating over remaining elements
for(j=0; j<numEmp-i-1; j++)
{
//Comparing total time
if(employees[j+1].TT > employees[j].TT)
{
//Swapping elements
temp = employees[j];
employees[j] = employees[j+1];
employees[j+1] = temp;

}
}
}
}

//Function that prints results
void writeReport(Employee employees[], int numEmp)
{
cout << “nn Employee Weekly Hours: n”;

//Printing header
cout << “nn ” << left << setw(20) << “Name:” << left << setw(5) << “S” << left << setw(5) << “M” << left << setw(5) << “T” << left << setw(5) << “W” << left << setw(5) << “T” << left << setw(5) << “F” << left << setw(5) << “S” << left << setw(10) << “TTL” << ” n”;

//Reading data from file
for(int i=0; i<numEmp; i++)
{
//Reading a line of data
cout << “n ” << left << setw(20) << employees[i].name << left << setw(5) << employees[i].sun << left << setw(5) << employees[i].mon << left << setw(5) << employees[i].tue << left << setw(5) << employees[i].wed << left << setw(5) << employees[i].thu << left << setw(5) << employees[i].fri << left << setw(5) << employees[i].sat << left << setw(10) << employees[i].TT << ” n”;
}

cout << “nn”;
}

//Main function
int main()
{
int numEmp;

//Array of employees
Employee *employees;

//Opening file in read mode
fstream fin(“employees.txt”, ios::in);

//Checking file status
if(fin.fail())
{
cout << “nn Error reading file… nn”;
return -1;
}

//Reading number of records from file
fin >> numEmp;

//Allocating memory
employees = new Employee[numEmp];

//Reading data from employees
readData(fin, employees, numEmp);

//Closing file
fin.close();

//Sort data
sortData(employees, numEmp);

//Writing results
writeReport(employees, numEmp);

cout << “nn”;
return 0;
}

______________________________________________________________________________________

Sample Output:

Question & Answer: of employee data for how many hours each employee worked for a week. Then for eac..... 2

Still stressed from student homework?
Get quality assistance from academic writers!