A few things.
The declaration of the "list" array should probably be done outside the definition of the struct, as you had done previously. You don't really want a static array...
CustomerRec list[MAX_CUSTOMERS]; // array named: "list" to hold customer records.
Secondly, you are trying to pass "list" (which is of type CustomerRec[]) when SSort is expecting a parameter of type double[]. You're also not passing it properly, you're passing element 25 of the array, which does not exist (0-24 are the elements in the array).
I also am not sure what the ElementNums is inside the struct for, it needs to be outside the struct. You can keep it as a global variable if you want but preferably make it local to main() function. Initialize it to 0 and every time you read a customer into the "list" array, increment it by 1.
You're also going to have to change your SSort function because you can't just say things like:
if (a[i] < a[minplace])
because a[i] is now a CustomerRec object, which is not something that can be compared using standard comparison operators. So you're going to have to add logic to work with the different cases, i.e sorting by zip, sorting by balance, etc. I would suggest passing another variable to SSort, type string, with values like "ZIP" and "BALANCE" and then using a case (or if/else) statement to determine which logic to use. Use comparisons like:
if (a[i].zipcode < a[minplace].zipcode)
Also, your sort algorithm is wrong. It should be this, found from Wikipedia:
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
|
/* a[0] to a[n-1] is the array to sort */
int iPos;
int iMin;
/* advance the position through the entire array */
/* (could do iPos < n-1 because single element is also min element) */
for (iPos = 0; iPos < n; iPos++) {
/* find the min element in the unsorted a[iPos .. n-1] */
/* assume the min is the first element */
iMin = iPos;
/* test against all other elements */
for (i = iPos+1; i < n; i++) {
/* if this element is less, then it is the new minimum */
if (a[i] < a[iMin]) {
/* found new minimum; remember its index */
iMin = i;
}
}
/* iMin is the index of the minimum element. Swap it with the current position */
if ( iMin != iPos ) {
swap(a, iPos, iMin);
}
}
|
Your first loop should go from pass=0 to pass=size, not pass=size-1. Your swapping should also only take place if minplace != pass.
Sooo...
Change the SSort function header to:
void SSort (CustomerRec a[ ], int size, string sortType);
Change the SSort function call to:
SSort(list, elementNums, "ZIP" /*or "BALANCE"*/);
As for your output, it's been like 5 years since I've done formatting like this but I think you can just increase "setw(7)" to something like "setw(15)" and it should right justify. You'll have to extend the other lines though. Either that or decrease the "setw(30)" for the last name.
I know this is a lot, tell me if you're confused by something.