In my code below you will see at the bottom my very lame attempt at sorting the vector array of employee records by name, alphabetically. My friend tried to tell me how I'm supposed to do it, but I can't make any sense of it really, so if someone could just put it into context for me that would help me out tremendously. I'm almost done with this program, just need to do this and one other thing that should be no problem, so please and thank you very much.
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;
class Records {
public:
string name;
int hours;
double rate;
int age;
double pay;
double tax;
double net;
/*double basepay;
double tax;
double netpay;*/
double basepay(int hours, double rate){
double pay, norm, over;
if (hours>40)
{
//ot = hours-40;
norm = 40*rate;
over = (hours-40)*(rate*1.5);
pay = norm+over;
}
else pay = hours*rate;
return pay;
}
double taxpaid(int age, double pay){
double percentage, tax;
if (age >= 55) percentage = 0.50;
else if (age < 55) percentage = 0.10;
tax = pay * percentage;
return tax;
}
double netpay(double pay, double tax){
double net = pay - tax;
return net;
}
};
double Records::basepay(int,double);
double Records::taxpaid(int,double);
double Records::netpay(double,double);
int main() {
cout << "\t\t\tMiser Corporation Payroll\n" << endl;
ifstream datafile; // Declare input file
datafile.open("datafile.txt"); // Open input file
// Check for error opening input file
if (!datafile)
{
cout << "Error opening file; program halted." << endl;
exit(1);
}
Records employee;
vector<Records> recordlist;
employee.pay = 0;
getline(datafile,employee.name);
datafile >> employee.hours >> employee.rate >> employee.age;
datafile.ignore();
while (datafile) {
cout << "Name: " << employee.name << endl;
cout << "Hours worked: " << employee.hours << endl;
cout << "Rate of pay: " << employee.rate << endl;
cout << "Age: " << employee.age << endl;
employee.pay = employee.basepay(employee.hours,employee.rate);
cout << "Base pay: " << employee.pay << endl;
employee.tax = employee.taxpaid(employee.age,employee.pay);
cout << "Tax paid: " << employee.tax << endl;
employee.net = employee.netpay(employee.pay,employee.tax);
cout << "Net pay: " << employee.net << endl;
cout << endl;
recordlist.push_back(employee);
getline(datafile,employee.name);
datafile >> employee.hours >> employee.rate >> employee.age;
datafile.ignore();
employee.pay = 0;
}
cout << endl;
int oldest = 0;
// Determine and print oldest employee(s):
for (int i=0; i<recordlist.size(); i++) {
if (recordlist[i].age > oldest){
oldest = recordlist[i].age;
}
}
cout << "Oldest employee(s): "
<< endl;
for (int o=0; o<recordlist.size(); o++) {
if (recordlist[o].age == oldest) {
cout << " " << recordlist[o].name << "; " << recordlist[o].age << endl;
}
}
cout << endl;
// Determine and print youngest employee(s):
int youngest = recordlist[0].age;
for (int i=0; i<recordlist.size(); i++) {
if (recordlist[i].age < youngest){
youngest = recordlist[i].age;
}
}
cout << "Youngest employee(s): "
<< endl;
for (int y=0; y<recordlist.size(); y++) {
if (recordlist[y].age == youngest) {
cout << " " << recordlist[y].name << "; " << recordlist[y].age << endl;
}
}
cout << endl;
// Determine and print employee who paid the most in taxes:
double hitax = 0; cout << "Employee who paid the most in taxes:" << endl;
for (int i=0; i<recordlist.size(); i++)
if (recordlist[i].tax > hitax) hitax = recordlist[i].tax;
for (int i=0; i<recordlist.size(); i++)
if (recordlist[i].tax == hitax) {
cout.precision(2);
cout << " " << recordlist[i].name << "; $";
cout << fixed << recordlist[i].tax << endl;
}
// Print an alphabetically sorted list of all employees:
sort (recordlist.begin(), recordlist.end());
datafile.close(); // Close input file
cout << "\n" << endl;
cout << "The payroll program is complete.";
return 0; // Close main program function
}
My friend's instructions:
If you want to use sort function to sort the vector you must define a less than function and pass it as the third parameter. It's very easy. Just create a new function that takes two employee objects by reference and returns a Bool. The whole function is one if else statement where you check whether the first parameter name is less than the second parameter name and return true or false accordingly. Then simply pass the name of the function to the sort function as if it was a variable -- don't put parentheses at the end of the name. It should be the last parameter passed. That's it.