Function for sorting strings not working

I've been trying to get this function to work for a while now but it's not happening.. Basically I need to make the function sort the strings in an alphabetical order.. This is what I have so far but when I run this function the name of the first person disappears.. I think it has something to do with my constant I have it set to 50 but it's actually supposed to be a user entered value... it's supposed to be the same value as total and I don't know how to make them the same value.... And also I need to link string x, string y, and int z together so that the names and ages don't get messed up

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
void sort_x (string x [], string y [], int z [], int total)
{
	int index;  // current spot in array being compared
	string temp;   // temporary value for swapping
	bool swapped;  // true if a swap was done, else false
	
	swapped = true;
	// continue making passes through the array
	// until no swaps have been done on a pass
	while (swapped)
	{
		swapped = false;
		// do one pass
		// step through array, swap pairs that are
		// out of order
		for (index = 0; index < total; index++)
		{
			// if the elements are out of order swap
			if (x [index] > x [index + 1])
			{
				temp = x [index];
				x [index] = x [index + 1];
				x [index + 1] = temp;
				swapped = true;
			}
		}
	}
}
Last iteration of each for-loop. The index==total-1
You dereference x[index+1]. That is x[total], out of range.

Whenever you swap x, you must swap y and z too.
Ok I fixed it and everything works now thank you!!
Last edited on
Topic archived. No new replies allowed.