Jul 9, 2010 at 7:43pm UTC
Moved from Beginners Forum.
Code:
#include <iostream>
#include <fstream> // File Input/Output Stream
#include<iomanip>
#include <windows.h>
using namespace std;
float sum=0;
float average=0;
float c=0;
int n=0;
int readalldata(char[][10], char[][15], long int[],char [],int[],float[],const int);
void findoth(int[],float[],int);
void findotp(float[],float[],float[],int);
void findregh(int[],int[],int);
void findregp(int[],float[],float[],int);
void findgp(float[],float[],float[],int);
void findtaxr(float[],float[],char[], int);
void findtaxa(float[],float[],float[],int);
void findnp(float[],float[],float[],int);
float findaverage(float[],int);
int sort(char[][10], char[][15],long int[],char[],int[],float[],float[],float[],float[],float[],float[],float[],int);
void printalldata(char[][10], char[][15],long int[],char[],int[],float[],float[],float[],float[],float[],float[],float[],int);
int main(){
const int MAXSIZE=100;
int i=0;
char FName[MAXSIZE][10], LName[MAXSIZE][15], mstat[MAXSIZE];
long int id[MAXSIZE];
int hw[MAXSIZE];
float oth[MAXSIZE];
int regh[MAXSIZE];
float hr[MAXSIZE], regp[MAXSIZE];
float otp[MAXSIZE], gp[MAXSIZE];
float taxr[MAXSIZE], taxa[MAXSIZE], np[MAXSIZE];
n=readalldata(FName, LName, id, mstat, hw, hr, MAXSIZE);
findoth(hw,oth, n);
findotp(oth, hr, otp, n);
findregh(hw, regh, n);
findregp(regh, regp, hr, n);
findgp(regp, otp, gp, n);
findtaxr(gp, taxr, mstat, n);
findtaxa(gp, taxa, taxr, n);
findnp(gp, np, taxa, n);
findaverage(np,n);
printalldata(FName,LName,id,mstat,hw,hr,oth,otp,regp,gp,taxr,np,n);
sort(FName,LName,id,mstat,hw,hr,oth,otp,regp,gp,taxr,np,n);
system("PAUSE");
}
int readalldata(char FName[][10], char LName[][15], long int id[], char mstat[], int hw[], float hr[], int n){
ifstream fin("payroll.in");
n=0;
while(fin>>FName[n]>>LName[n]>>id[n]>>mstat[n]>>hw[n]>>hr[n]) n++;
fin.close();
return n;
}
void findoth(int hw[],float oth[], int n){
for(int i=0; i<n; i++){
if(hw[i]>40)oth[i]=hw[i]-40;
else oth[i]=0;
}
}
void findotp(float oth[], float hr[], float otp[], int n){
for(int i=0; i<n; i++){
otp[i]=oth[i]*hr[i]*1.5;
}
}
void findregh(int hw[], int regh[], int n){
for(int i=0; i<n; i++){
if(hw[i]>40) regh[i]=40;
else regh[i]=hw[i];
}
}
void findregp(int regh[], float regp[], float hr[], int n){
for(int i=0; i<n; i++){
regp[i]=regh[i]*hr[i];
}
}
void findgp(float regp[], float otp[], float gp[], int n){
for(int i=0; i<n; i++){
gp[i]=regp[i]+otp[i];
}
}
void findtaxr(float gp[], float taxr[], char mstat[], int n){
for(int i=0; i<n; i++){
if(gp[i]>1000.00)taxr[i]=.30;
else if(gp[i]>800.00) taxr[i]=.20;
else if(gp[i]>500.00) taxr[i]=.10;
else taxr[i]=0;
if (mstat[i]=='S') taxr[i]=(taxr[i]+.05);
else if (mstat[i]=='s') taxr[i]=(taxr[i]+.05);
else if (mstat[i]=='H' && gp[i]>500) taxr[i]=(taxr[i]-.05);
else if (mstat[i]=='h' && gp[i]>500) taxr[i]=(taxr[i]-.05);
else if (mstat[i]=='M') taxr[i]=(taxr[i]*1);
else if (mstat[i]=='m') taxr[i]=(taxr[i]*1);
}
}
void findtaxa(float gp[], float taxa[], float taxr[], int n){
for(int i=0; i<n; i++){
taxa[i]=gp[i]*taxr[i];
}
}
void findnp(float gp[], float np[], float taxa[], int n){
for(int i=0; i<n; i++){
np[i]=gp[i]-taxa[i];
}
}
float findaverage(float np[], int n){
for(int i=0; i<n;i++){
sum=sum + np[i];
c=c+1;
average = sum/c;
}
}
int sort(char FName[][10], char LName[][15], long int id[], char mstat[], int hw[],
float hr[], float oth[], float otp[], float regp[],
float gp[], float taxr[], float np[], int n){
int i,j;
for(int i=0; i<n-1; i++){
for(j=n-1; j>i; j--){
if(np[j]<np[j-1]){
float hold = (FName[j][10], LName[j][15], id[j], mstat[j], hw[j],
hr[j], oth[j], otp[j], regp[j],
gp[j], taxr[j], np[j]);
(FName[j][10], LName[j][15], id[j], mstat[j], hw[j],
hr[j], oth[j], otp[j], regp[j],
gp[j], taxr[j], np[j]) = (FName[j-1][10], LName[j-1][15], id[j-1], mstat[j-1], hw[j-1],
hr[j-1], oth[j-1], otp[j-1], regp[j-1], gp[j-1], taxr[j-1], np[j-1]);
(FName[j-1][10], LName[j-1][15], id[j-1], mstat[j-1], hw[j-1],
hr[j-1], oth[j-1], otp[j-1], regp[j-1],
gp[j-1], taxr[j-1], np[j-1]) = hold;
}//
}//
}//
cout<<"Sorted Data"<<endl<<endl;
cout<<setw(8)<<left<<"FIRST"<<setw(9)<<"LAST"<<setw(6)<<"ID"<<setw(6)<<"STAT"<<setw(4)<<"HW"<<
setw(5)<<"HR"<<setw(4)<<"OTH"<<setw(6)<<"OTP"<<setw(8)<<"REGP"<<setw(8)<<"GROSS"<<setw(6)<<"TAX"<<
setw(8)<<"NET"<<endl;
cout<<setw(8)<<left<<"====="<<setw(9)<<"===== "<<setw(6)<<"===="<<setw(6)<<"===="<<setw(4)<<"==="<<
setw(5)<<"==="<<setw(4)<<"==="<<setw(6)<<"==="<<setw(8)<<"====="<<setw(8)<<"====="<<setw(6)<<"===="
<<setw(3)<<"====="<<endl;
for(i=0;i<n;i++){
cout<<setw(8)<<FName[i]<<setw(9)<<LName[i]<<setw(6)<<id[i]<<setw(6)<<mstat[i]<<setw(4)<<hw[i]
<<setw(5)<<hr[i]<<setw(4)<<oth[i]<<setw(6)<<otp[i]<<setw(8)<<regp[i]
<<setw(8)<<gp[i]<<setw(6)<<taxr[i]<<setw(1)<<"$"<<np[i]
<<endl<<endl;
}
}
void printalldata(char FName[][10], char LName[][15], long int id[], char mstat[], int hw[],
float hr[], float oth[], float otp[], float regp[],
float gp[], float taxr[], float np[], int n){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
cout<<"\t \t \t MY PAYROLL PROGRAM"<<endl<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
cout<<"Unsorted Data"<<endl<<endl;
cout<<setw(8)<<left<<"FIRST"<<setw(9)<<"LAST"<<setw(6)<<"ID"<<setw(6)<<"STAT"<<setw(4)<<"HW"<<
setw(5)<<"HR"<<setw(4)<<"OTH"<<setw(6)<<"OTP"<<setw(8)<<"REGP"<<setw(8)<<"GROSS"<<setw(6)<<"TAX"<<
setw(8)<<"NET"<<endl;
cout<<setw(8)<<left<<"====="<<setw(9)<<"===== "<<setw(6)<<"===="<<setw(6)<<"===="<<setw(4)<<"==="<<
setw(5)<<"==="<<setw(4)<<"==="<<setw(6)<<"==="<<setw(8)<<"====="<<setw(8)<<"====="<<setw(6)<<"===="
<<setw(3)<<"====="<<endl;
for(int i=0; i<n;i++){
cout<<setw(8)<<FName[i]<<setw(9)<<LName[i]<<setw(6)<<id[i]<<setw(6)<<mstat[i]<<setw(4)<<hw[i]
<<setw(5)<<hr[i]<<setw(4)<<oth[i]<<setw(6)<<otp[i]<<setw(8)<<regp[i]
<<setw(8)<<gp[i]<<setw(6)<<taxr[i]<<setw(1)<<"$"<<np[i]
<<endl<<endl;
}
cout<<setw(8)<<"The Total Net Pay is: $"<<sum<<endl;
cout<<setw(8)<<"The Average Net Pay is: $"<<average<<endl<<endl;
}
My data all calcs properly. My non-sorted data is fine. Sorted is wrong. It only sorts Netpay column (leaves the others in same order). How do I get the output to keep all of the related columns with the netpay and still sort by netpay?
Example:
Unsorted Data
Joe Smith 1234 M 43 10 3 45 400 445 0 $445
Mary Jones 2345 S 40 12.75 0 0 510 510 0.15 $433.5
As is Sorted Output comes out:
Joe Smith 1234 M 43 10 3 45 400 445 0 $433.5
Mary Jones 2345 S 40 12.75 0 0 510 510 0.15 $445
Should come out:
Mary Jones 2345 S 40 12.75 0 0 510 510 0.15 $433.5
Joe Smith 1234 M 43 10 3 45 400 445 0 $445
Any ideas would be great.
Thanks!