Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program to accept list of name (Frist Name and last name) from user. And print this name in alphabetic sorted order with respect to Last name and First name.
Input
Darshit shah
Jitendra kala
Darshit dave

Output
Dave darshit
Kala Jitendra
Shah darshit

What I have tried:

C++
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;
	// Do NOT allocate twice!!
	// names = new char*[Number_of_names];
	// for (int i = 0; i < Number_of_names; i++) {
	//	 names[i] = new char[20];
	// }
	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);

	// dont forget to free allocated memory!
	name_list_free(names, Number_of_names);

	return 0;
}
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;
void name_list_lastsort(char**names, int n)
{
	int* sep = new int[n];  // allocate array of n int

	// find last names
	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;
	}

	// sort names
	for (int i=0; i
Posted
Updated 12-Sep-21 10:43am
v3
Comments
Patrice T 8-Sep-21 13:52pm    
Show exact error message.
Patrice T 8-Sep-21 13:58pm    
your cade was corrupted, please try again in the pre tag.

C and C++ are old languages: unlike more modern ones like C# which don't care about definition order, they both insist that functions must be defined before they are used.
So when your main function tried to call name_list_input, name_list_output, name_list_lastsort, and name_list_free without a earlier defintion, the compiler complains that it can't find them in the current scope.

That's where forward definitions come in: What are Forward declarations in C++ - GeeksforGeeks[^]
 
Share this answer
 
Comments
OriginalGriff 9-Sep-21 2:30am    
You are kidding, right?
Follow the link I gave you and it explains - in detail - what you have to do.
But in brief:
1) Copy the header line from a function definition. For example:
void name_list_lastsort(char**names, int n)

2) Paste it above the definition of main:
void name_list_lastsort(char**names, int n)

3) Add a semicolon to the end:
void name_list_lastsort(char**names, int n);


Now, why the heck can't you do that without step-by-step instructions?
Richard MacCutchan 9-Sep-21 4:09am    
Or better still move the functions above main so you do not need to define them twice.
OriginalGriff 9-Sep-21 4:46am    
yeah, but if you learn about forward definitions, you aren't "hung up" on a forced ordering for methods to ensure they can see each other.
They can be grouped together.

But you know that, I was just typing for the OP ... :D
Richard MacCutchan 9-Sep-21 4:52am    
Of course. But sometimes I just need to vent to someone who actually understands. :)
OriginalGriff 9-Sep-21 5:17am    
They do get you like that, some of them. :sigh:
OriginalGriff already addressed your problem. In addition, I suggest you using the facilities the C++ programming language and its library gently provide, like string objects, containers, algorithms.
 
Share this answer
 
The source code presented above is incomplete and it is not clear where the problem lies.

As I can see, proposed solutions were copied and pasted from me.
I wonder why the solution was not accepted here.

How do I sort last names alphabetically and print them before first[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900