As Richard Mc Cutchan pointed out, memory is allocated unnecessarily. As the pointer is overwritten, a memory leak occurs because the memory can no longer be released. In general, allocated memory should be released again somewhere.
The process should roughly look like this:
int main()
{
int Number_of_names;
cout << "How many names do you want to enter? ";
cin >> Number_of_names;
cin.ignore(1000, '\n');
char** names = NULL;
names = name_list_input(Number_of_names);
name_list_output(names, Number_of_names);
name_list_lastsort(names, Number_of_names);
name_list_output(names, Number_of_names);
name_list_free(names, Number_of_names);
return 0;
}
I´m not Shure if you are not allowed to use strlen which would make sense.
int mystrlen(char* s)
{
int i;
for (i=0; s[i] != NULL; i++);
return i;
}
int compare_C_strings(char* s1, char* s2)
{
int lenS1 = mystrlen(s1);
int lenS2 = mystrlen(s2);
int n = (lenS1 <= lenS2) ? lenS1 : lenS2;
for (int i = 0; i<n; i++) {
if (s1[i] > s2[i])
return 1;
if (s1[i] < s2[i])
return -1;
}
return 0;
}
The alphabetical order of the last names could look like this:
void name_list_lastsort(char**names, int n)
{
int* sep = new int[n];
int j = 0;
for (int i = 0; i < n; i++) {
for (j = 0; (names[i][j] != NULL) & (names[i][j] != ' '); j++);
sep[i] = (names[i][j] == ' ') ? j+1 : 0;
}
for (int i=0; i<n-1; i++) {
for (j=i+1; j<n; j++) {
switch (compare_C_strings(names[i] + sep[i], names[j] + sep[j])) {
case 1: { ... }
break;
case 0: { ... }
break;
}
}
}
delete []sep; }