Please Help with Sorting!!!!!!!!!!!

The Program runs fine with just minor problem. I have written the program but it doesn't seem to sort the names instead it just publish them as entered. Please someone point out the problem that is stopping the sorting. PRONTO PLEASE!!!!

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <stdlib.h>
using namespace std;

void Sorted_Names(char Total_Names[][16], int TotalNum)
{
	char i;
	bool Sorted;
	char Temp;
	
	do
		{
			Sorted = true;
			TotalNum--;
			for(i = 0; i < TotalNum; i++)
				if(Total_Names[i] > Total_Names[i + 1])
					{
						Temp	= Total_Names[i][16];
						Total_Names[i][16] = Total_Names[i + 1][16];
						Total_Names[i + 1][16]	= Temp;
						Sorted		= false;
					
					}
				else;
		}
			while(!Sorted);
}


void Enter_Name(int XY)
{
	int	i=0;
	int		Num;
	char	Total_Names[20][16] = {};
	char	Name;

	for(Num = 1; Num <= XY; Num++)
		{
			cout << "Enter the " << Num << " Name: ";
			cin >> Total_Names[Num - 1];
//                      cin >> Name;
//			Total_Names[Num - 1] = Name;
		}
	cout << "\tOriginal Names" << endl;
	for (int i = 0; i < XY; i++)
		cout << '\t' << Total_Names[i] << endl;

	cout << "\n\t Sorted Names" << endl;
	Sorted_Names(Total_Names, XY);
	for(int j = 0; j < XY; j++)
		cout << "\t" << Total_Names[j] << endl;
}

int main()
{
	int Ask_Num;

	cout << "How many Names You Wish to Enter (1 to 20): ";
	cin >> Ask_Num;
		if(Ask_Num <= 0)
			{
				cout << "Not Valid Number, Program Terminated" << endl;
				exit(0);
			}
		else if(Ask_Num > 20)
			{
				cout << "Not Valid Number, Program Terminated" << endl;
				exit(0);
			}
		Enter_Name(Ask_Num);
}




This is just an exact copy of the output:

Original Names

Zoe
Susan
Jack
Sparrow
Clark
Kent


Sorted Names //***NOT SORTED AT ALL, AS APPEARED IN OUTPUT, WHEN COMPILED


Zoe
Susan
Jack
Sparrow
Clark
Kent

Press any key..............

Last edited on
if(Total_Names[i] > Total_Names[i + 1])

I'm no expert, but I'm pretty sure this is comparing the memory addresses of the two c-strings. At any rate, it doesn't do what you think it does. The expression probably always evaluates as false, which is why the sorting is never done.

Look into the cstring functions: http://www.cplusplus.com/reference/cstring/
Last edited on
NO it works, but not as epected.
Main problem with lines 18-21 where he swapping character at index 16 in 16 character arrays, aka randomly move some memory outside array bounds.
I replaced line 22 with cout << "Did something!" << endl; and ran the program. Never got "Did something" so I'm going to say that it didn't work.

He still needs to use strcpy on those lines you pointed out though.
Last edited on
Because array is already sorted. Replace < with > to sort in descending order and it should fire at least once. Actually you should get an infinite loop here
I just noticed that Total_Names[i + 1] is also outside the bounds of the array the first time going through the loop.
Topic archived. No new replies allowed.