names alphabetical order

closed account (4wpL6Up4)
Hi,

I feel a bit lost in this code.
I have to complete it according to the direction stated in the comments.
I have done some parts but I am not sure if they are correct.
I have left blank the parts where I am not really sure what to do.
Any help/hint would be appreciated.

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
 #include <iostream>
#include <cstring>
#include<algorithm>

using namespace std;
const int BUFLEN = 100; 
void sort(char *friendList[], int n); 
void print(char *friendList[], int n); 
void terminate(char *friendList[], int n);
const int AMOUNT = 5;
int main()
{
	char*	friends[AMOUNT];
	char buff[BUFLEN] = { "" }; 
	int count = 0;

	while (count < AMOUNT)
	{
		cout << "Name	a	friend:	";
		cin.getline(buff, BUFLEN); 
		friends[count] = .	.	. //	WRITE	CODE	allocating memory	to the	string
		//	WRITE	CODE	that	adds	loaded name	to current	location	in	the	vector
			++count;
	}
	sort(friends, count); 
	print(friends, count);
	terminate(friends, count); 	
	return 0;
}
void sort(char *friendList[], int n)
{
	// WRITE FUNCTION that sorts the vector strings in alphabetical order!
	int z = 0;
	cin.get(friendList,n)

	sort(friendList[0], friendList[n]);

	for (int y = 0; y < n; y++)
	{
		cout << friendList[n] << endl;
	}

	
}
void print(char *friendList[], int n)
{
	// WRITE FUNCTION that presents ā€˜nā€™ names from the vector on screen!
	cout << friendList[n] << endl;
}
void terminate(char *friendList[], int n)
{
	// WRITE FUNCTION that releases all dynamically allocated memory!
}
First, comment out the call to sort() at line 25. This will let you debug the code that reads the names and prints them out before you try to debug sort(), which will be harder.

At line 21, you need to allocate space for a copy of the string in buff:
- use strlen() to get the actual length of the string if buff.
- use malloc() to allocate space for the copy. Assign this space to friends[count]. Note that you need to allocate ONE MORE BYTE than the length of the string in buff as returned by strlen. That's because you need room for the null byte that indicates the end of the string and strlen() doesn't include that.
- use strcpy() to copy the string from buff to the newly allocated space.

In print(), replace line 48 with the loop at lines 38-41, then delete lines 38-41. After all, sort() should just sort the array. It should NOT print it.

When all of this is done, then program should read 5 names and then print them out in the same order. Get that working, and then we can fix sort().
Topic archived. No new replies allowed.