Here's my code. (Lines of interest: 49, 138, and 162; rest of the code works fine.)

#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <conio.h>
using namespace std;

class Records {
public:
    string name;
    int hours;
    double rate;
    int age;

    double pay;
    double tax;
    double net;

    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;
    }

};

vector<Records> printPayroll( vector<Records> v );
bool lessThan( const Records&, const Records& );

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;
        }

   printPayroll( recordlist[] );

    cout << "\nPrint employee records in alphabetical order?"
    << "\n\tPress any key_" << endl;

    cout << endl;
    getch();

    // Print an alphabetically sorted list of all employees:
   sort( recordlist.begin(), recordlist.end(), lessThan );
   for (int i=0; i<recordlist.size(); i++) {
        cout << "Name: " << recordlist[i].name << endl;
        cout << "Hours worked: " << recordlist[i].hours << endl;
        cout << "Rate of pay: " << recordlist[i].rate << endl;
        cout << "Age: " << recordlist[i].age << endl;
        cout << endl;
   }

    datafile.close(); // Close input file
    cout << "\n" << endl;
    cout << "The payroll program is complete.";
    return 0; // Close main program function
}

vector<Records> printPayroll ( vector<Records> v ) {
    int iv = v.size();
    for (r = 0; r<iv; r++){
        cout << "Name: " << v[r].name << endl;
        cout << "Hours worked: " << v[r].hours << endl;
        cout << "Rate of pay: " << v[r].rate << endl;
        cout << "Age: " << v[r].age << endl;
        cout << endl;
    }
}

bool lessThan ( const Records& a, const Records& b) {
    return a.name < b.name;
}

Recommended Answers

All 2 Replies

Attempting a compile got me some errors. On line 138, you have printPayroll( recordlist[] );. There is no need for these empty brackets unless you're declaring an array. Remove them and that error should be solved.

On line 164, I get the error that r has not been declared in this scope. On your compiler (which I'm assuming is old), you may not need to declare r as an int, but I suggest replacing r = 0 with int r = 0 just to be sure.

The function printPayroll() has a return type of vector<Records> but you have no return statement in that function. Since it's only printing out information, you probably meant for the return type to be void.

To add to Tumlee's comments, I would suggest your prototype for the printPayroll function should be like this:

void printPayroll ( const vector<Records>& v );

Use the passing by const-reference to avoid copying such a large object as a vector.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.