Arranging chars problem

Basically, I am trying to have employee names follow their net pay when it is arranged into ascending order. I was able to get the net pays in ascending order, but the names do not follow with their respective net pay. I was thinking about doing some if/else statements, but then I think my code would be too long and complicated. Any suggestions? Here is what I have thus far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
float findorder(float net[], char fn[100][14], char ln[100][15], int n){
      for(int i=0;i<n-1;i++){
       for(int p=n-1;p>i;p--){
            if (net[p]<net[p-1]){
                float tempnet=net[p];
                net[p]=net[p-1];
                net[p-1]=tempnet;
            }//NET ORDER
            if  (fn[p]<fn[p-1]){
                char tempfn=fn[100][14];
                fn[p]=fn[p-1];
                fn[p-1]=fn[tempfn];
            }//FN ORDER
            if  (ln[p]<ln[p-1]){
                char templn=ln[100][15];
                ln[p]=ln[p-1];
                ln[15-1]=ln[templn];
            }//LN ORDER
        }//FOR
    }//ORDER
}//FINDORDER 


With these errors:
1
2
3
4
5
6
C:\Users\Eric\...\mod4b.cpp||In function `float findorder(float*, char (*)[14], char (*)[15], int)':|
C:\Users\Eric\...\mod4b.cpp|147|error: ISO C++ forbids assignment of arrays|
C:\Users\Eric\...\mod4b.cpp|148|error: ISO C++ forbids assignment of arrays|
C:\Users\Eric\...\mod4b.cpp|152|error: ISO C++ forbids assignment of arrays|
C:\Users\Eric\...\mod4b.cpp|153|error: ISO C++ forbids assignment of arrays|
||=== Build finished: 4 errors, 0 warnings ===| 


In researching this, I read that this error is because making an array equal another one could cause crashes because their length may vary. It was also suggested to use a memcpy function, but I haven't used memcpy before. Would this be my best bet to get this finished?

Thanks in advance!
Right here:
char tempfn=fn[100][14];

fn is an array 100*14. By assigning tempfn to element 100 (the 101st element in the sequence) and element 14 (the 15th element in the sequence) you are stepping out of bounds of the array.

I've just also noticed that before that, in the function header, you have char fn[100][14] which may cause some problems.

What I would do to organize data would be to make a structure containing net pay and names.
1
2
3
4
5
6
struct Employee
{
float net;
char fn[14];
char ln[15];
} Employees[100];


Now you could pass just the structure into your findorder function and then you only need to sort one time, not once for each of your entries.

example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void findorder(Employee list[], int n) // n represents the number of elements to sort, 
{
	bool out_of_order = true;
	while (out_of_order) {
		out_of_order = false;
		for (int i=0; i < n-1; i++) {
			if (list[i].net > list[i+1].net) {
				out_of_order = true;
				Employee temp = list[i];
				list[i] = list[i+1];
				list[i+1] = temp;
			}
		}
	}
}


Granted this code is not the most efficient ordering algorithm, nor does it return the array back to the main function. But it does display how much easier it is to sort a structure than 3 parallel arrays.

Now, if I were to make minimal changes to your function, I notice that you seem to order each array independantly. This means that they will all be in their own orders from lowest value to highest which means the names and net pay will not correlate. If you want to sort by net pay, then do the if (net[p]<net[p-1]){ condition and then swap all three arrays in that if statement. This means that you should not use if (fn[p]<fn[p-1]){ or if (ln[p]<ln[p-1]){.
Last edited on
Is there any special reason you are using char** instead of std::string or vector<string>? I mean I think it would be easier for you to deal with your sorting.

Your errors occur because you are trying to assign an array to an array by it's pointer:
fn[p]=fn[p-1];
What's the type of fn? char**, that is pointer to pointer of char. So fn[p] is a pointer to char (char*) so you cannot copy c-strings this way. At best this copies the pointer. Not what you want I bet.

fn[p-1]=fn[tempfn];
Here tempfn is a char, and more specifically the char stored inside your 2D array at some location, so what are you trying to do here?

I would suggest to get rid of all this arrays and use vector<string> instead.
If you insist on char* maybe this will help:
http://www.cprogramming.com/tutorial/lesson9.html
Thank you both for your replies, they are most appreciated!

Stewbond, this is for a C++ class that I am taking, and we haven't reached structures yet. I would feel uncomfortable turning in a project using a structure when I'm not even sure what it does, yet. But, thanks for your final paragraph, as I will be able to use that information in dealing with this program. Also, I definitely will keep your post in mind when we do get to that point, though, so thank you kindly!

eypros, thank you very much for your suggestions. With the info you provided, as well as the link, I think I can fix the problem.

I will post back if and when I get the program to work. Again, thank you both!
closed account (zb0S216C)
Arrays cannot be assigned to each other because the length of an array is constant. Therefore, assigning an array of 50 elements to an array of 30 elements would mean the left-hand array would have to re-size itself to accommodate the new length; which isn't allowed.

Another reason is that C++ doesn't know the length of the array; hence the reason why you get an exception when you over-step the boundaries of an array.

Wazzak
Last edited on
Thanks a lot to all of those that helped. I managed to get it to work!

And thank you, Framework. Your post actually spelled out exactly what I was doing wrong in an easy to understand way. I appreciate it!
Topic archived. No new replies allowed.