Sorting, Help!
May 29, 2011 at 4:46am UTC
I'm going to get to the point. I am having problems understanding how to sort. Please explain how do I sort the netpay of the below code.
Thanks in advance!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
//---------------------------------------------------------------------------\\
class payroll
{
ifstream fin;
int n;
char employeename[20];
int hoursworked,overtime;
double hourlyrate,overtimepay,regularpay,grosspay,taxrate,taxamount,netpay;
double totalnetpay,avgnetpay;
void calculategrosspay();
void calculatetax();
void calculatenetpay();
void findavgnetpay();
void printheadings();
void printdata();
public :
payroll();
~payroll();
void printreport();
};
//---------------------------------------------------------------------------\\
payroll::payroll()
{
fin.open("payrollclass.txt" );
}
//---------------------------------------------------------------------------\\
payroll::~payroll()
{
fin.close();
}
//---------------------------------------------------------------------------\\
void payroll::calculategrosspay()
{
if (hoursworked > 40)
{
overtime = hoursworked - 40;
regularpay = hoursworked * hourlyrate;
overtimepay = overtime * (hourlyrate * 1.5);
grosspay = regularpay + overtimepay;
}
else
{
grosspay = hoursworked * hourlyrate;
regularpay = grosspay;
overtimepay = 0;
}
}
//---------------------------------------------------------------------------\\
void payroll::calculatetax()
{
taxrate = .30;
taxamount = grosspay * taxrate;
}
//---------------------------------------------------------------------------\\
void payroll::calculatenetpay()
{
netpay = grosspay - taxamount;
totalnetpay = totalnetpay + netpay;
}
//---------------------------------------------------------------------------\\
void payroll::findavgnetpay()
{
avgnetpay = totalnetpay / n;
}
//---------------------------------------------------------------------------\\
void payroll::printheadings()
{
cout << setw(40)<<"-Dr. Ebrahimi Payroll Institute-" <<endl;
cout << "---------------------------------------------------------" << endl;
cout << " NAME HW HR OT-PAY GROSS TAX NETPAY" << endl;
cout << "---------------------------------------------------------" << endl;
}
//---------------------------------------------------------------------------\\
void payroll::printdata()
{
cout << setprecision(2) << setiosflags(ios::fixed|ios::showpoint);
cout << setw(6) << employeename << setw(6) << hoursworked << setw(8) <<
hourlyrate << setw(8) << overtimepay << setw(8) << grosspay <<
setw(8) << taxamount << setw(8) << netpay << endl;
}
//---------------------------------------------------------------------------\\
void payroll::printreport()
{
n = 0;
totalnetpay = 0;
printheadings();
while (fin >> employeename >> hoursworked >> hourlyrate)
{
calculategrosspay();
calculatetax();
calculatenetpay();
printdata();
n++;
}
findavgnetpay();
cout << endl << "The average net pay for " << n << " employees is " << avgnetpay << endl;
}
//---------------------------------------------------------------------------\\
int main()
{
payroll employee;
employee.printreport();
system ("pause" );
}
//---------------------------------------------------------------------------\\
May 29, 2011 at 7:11am UTC
Well, you can't sort them if you don't store them anywhere..
Generally, your structure is a bit wrong. "payroll" is at the same time a single employee (since it has name, hours worked, etc) and the whole group (since it has n, totalnetpay, etc).
You should split it in two:
1 2 3 4 5 6 7 8 9 10
struct employee{
char employeename[20];
int hoursworked,overtime;
double hourlyrate,overtimepay,regularpay,grosspay,taxrate,taxamount,netpay;
void calculategrosspay();
void calculatetax();
void calculatenetpay();
void printdata();
};
and
1 2 3 4 5 6 7 8 9
struct workplace{
std::vector<employee> workers;//this could be "employee workers[big_number]; int n;" if you don't know vectors
double totalnetpay, avgnetpay;
void readdata();
void sortdata();
void printheadings();
void printreport();
};
Something like that anyway..
Topic archived. No new replies allowed.