C++ Program that works with structures, array of structures, files, sorting, and
ID: 3812870 • Letter: C
Question
C++ Program that works with structures, array of structures, files, sorting, and formatting using the given instructions. Thank you!
Sample input File: (sample output is below)
1000 George Washington 10000
2000 John Adams 15000
1212 Thomas Jefferson 34000
1313 Abraham Lincoln 45000
1515 Jimmy Carter 78000
1717 George Bush 80000
objectives: Work with structures, array of structures, files, sorting, and formatting A company keeps its employee records in a file. Each line in the file includes a single employee record (id, first name, last name, salary). Write a C++ programthat prints several reports of employee data You are asked to process the data in the file be storing each record in an array of structures. You may assume thatthe number of employees will never exceed 100. Your program must print two reports All employee records sorted by last name. All employee records sorted by salary. Find and print the total payroll. Hints: Write a function to load the arrayof structures (make sure the name is in the right format) Write a function to sort the array by last name. Write a function to sort the array by salary o Use the same swap function for both sort functions Write a function to printthe report. This function can be used for both reports. Write a function to calculate and return the total payroll.Explanation / Answer
/*
* payroll.cpp
*
* Created on: 07-Apr-2017
* Author: kasturi
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
struct Employee
{
int id;
string firstName;
string lastName;
double salary;
};
void loadArray(struct Employee *employee, int &counter)
{
ifstream in("employee.txt");
string line;
counter = 0;
while(getline(in, line))
{
istringstream iss(line);
if(!(iss>>employee[counter].id>>employee[counter].firstName>>employee[counter].lastName>>employee[counter].salary))
{
cout<<"Something went wrong!";
exit(1);
}
counter++;
}
in.close();
}
void swap(struct Employee *e1, struct Employee *e2)
{
struct Employee *temp = new Employee;
temp->id = e1->id;
temp->firstName = e1->firstName;
temp->lastName = e1->lastName;
temp->salary = e1->salary;
e1->id = e2->id;
e1->firstName = e2->firstName;
e1->lastName = e2->lastName;
e1->salary = e2->salary;
e2->id = temp->id;
e2->firstName = temp->firstName;
e2->lastName = temp->lastName;
e2->salary = temp->salary;
}
void sortByName(struct Employee employee[], int size)
{
for(int i=0; i<size-1; i++)
{
string name1 = employee[i].lastName;
string name2 = employee[i+1].lastName;
if(name1.compare(name2) > 0)
{
swap(employee[i], employee[i+1]);
}
}
}
void sortBySalary(struct Employee *employee, int size)
{
for(int i=0; i<size-1; i++)
{
if(employee[i].salary>employee[i+1].salary)
{
swap(employee[i], employee[i+1]);
}
}
}
void printReport(struct Employee *employee, int size)
{
cout<<"ID "<<"Name "<<"Salary"<<endl;
cout<<"------------------------------------------"<<endl;
for(int i=0; i<size; i++)
{
string name = employee[i].lastName+", "+employee[i].firstName;
cout<<to_string(employee[i].id)<<setw(20)<<name<<setw(5)<<"$";
cout<<fixed<<setprecision(2)<<employee[i].salary<<endl;
}
cout<<endl;
}
double getPayroll(struct Employee *employee, int size)
{
double total = 0.00;
for(int i=0; i<size; i++)
{
total += employee[i].salary;
}
return total;
}
int main()
{
struct Employee employees[20];
int size = 0;
loadArray(employees, size);
printReport(employees, size);
sortByName(employees, size);
printReport(employees, size);
sortBySalary(employees, size);
printReport(employees, size);
double payroll = getPayroll(employees, size);
cout<<" Total Payroll: $"<<payroll<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.