0

My whole code is below. I need to print an alphabetically sorted list towards the end right now, that's where I'm at... you can see my measly attempt, but I clearly don't really know what I'm doing with this sorting algorithm... Do I need to make a new vector comprised of only the names/strings in order to do this? How is this going to work.

Thank you kindly in advance.

#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
}
2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by Lerner
0

The thing is I'm pretty sure I need to have THIS vector array sorted -- i.e., the one I already have set up with all the employees' info.

0

IIf you are going to use sort() then you need to overload the less than operator for the Records class or create some other function to compare two Record objects----say compareNames() or something.

If you are going to do some othe sort routine, say a bubble sort or whatever, to sort alphabetically by name, then compare names directly, but swap the respective record objects.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.