trouble with my sort function
Feb 21, 2011 at 8:44pm UTC
Can you guys take a look at my sort function? I need to sort netpays in ascending order while displaying it's lastname, ot pay, and tax.I can not get it to compile; it gives me an error message "invalid conversion from `char*' to `char'" for my char and 3 doubles. How can I fix this?
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
void payroll::sortdata() {
int i,j;
const int n = 100;
double netpay[100];
double anetpay[n];
double overtimepay[100];
double aovertimepay[n];
double taxamount[100];
double ataxamount[n];
char lastname[100];
char alastname[n];
double temp, tempb, tempc;
char tempa;
int sorted = 0;
for ( i = 0; i < n; i++ ){
alastname[i] = lastname + i;
aovertimepay[i] = overtimepay + i;
ataxamount[i] = taxamount +i;
anetpay[i] = netpay + i; } //for
while (!sorted){
sorted = 1;
for ( j = 0; j < n -1; j++ ){
if ( anetpay[j] > anetpay[j+1] ){
temp = anetpay[j];
anetpay[j] = anetpay[j+1];
anetpay[j+1] = temp;
tempa = alastname[j];
alastname[j] = alastname[j+1];
alastname[j+1] = tempa;
tempb = aovertimepay[j];
aovertimepay[j] = aovertimepay[j+1];
aovertimepay[j+1] = tempb;
tempc = ataxamount[j];
ataxamount[j] = ataxamount[j+1];
ataxamount[j+1] = tempc; sorted = 0;
}//if
}//for
}//while
}//sort
Feb 21, 2011 at 11:59pm UTC
Have you learned about structs, strings, vectors and the STL sort() function yet?
Feb 22, 2011 at 2:16am UTC
Nope. I dk what a STL sort is
Feb 22, 2011 at 3:18am UTC
It would be nice if you could specify the line so I/we don't have to go looking.
Feb 22, 2011 at 3:32am UTC
I am unsure of the declaration lines 2-11 and 18-21. I think the logic to my sort lines 26-43 is correct.
Feb 22, 2011 at 4:20am UTC
I just made an update:
declaring the prototype outside the payroll class:
void sortdata(string[], double [], double [], double []);
New sort function def:
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
void sortdata(string lastname[], double overtimepay[], double taxamount[], double netpay[] ){
int i,j;
const int n = 100;
double *anetpay[n];
double *aovertimepay[n];
double *ataxamount[n];
string *alastname[n];
double *temp, *tempb, *tempc;
string *tempa;
int sorted = 0;
for ( i = 0; i < n; i++ ){
alastname[i] = lastname + i;
aovertimepay[i] = overtimepay + i;
ataxamount[i] = taxamount +i;
anetpay[i] = netpay + i; } //for
while (!sorted){
sorted = 1;
for ( j = 0; j < n -1; j++ ){
if ( *anetpay[j] > *anetpay[j+1] ){
temp = anetpay[j];
anetpay[j] = anetpay[j+1];
anetpay[j+1] = temp;
tempa = alastname[j];
alastname[j] = alastname[j+1];
alastname[j+1] = tempa;
tempb = aovertimepay[j];
aovertimepay[j] = aovertimepay[j+1];
aovertimepay[j+1] = tempb;
tempc = ataxamount[j];
ataxamount[j] = ataxamount[j+1];
ataxamount[j+1] = tempc; sorted = 0;
}//if
}//for
}//while
}//sort
and calling it in the main block
sortdata(lastname,overtimepay,taxamount, netpay);
But it still will not sort. Are the above blocks correct>?
Feb 22, 2011 at 6:41pm UTC
Guys, any ideas on the above?
Topic archived. No new replies allowed.