Help With Alphabetizing Using An Array

Hi there! I am a beginner C++ Student and am currently having trouble with alphabetizing an array. I have this code where a user will enter 20 book Authors/Titles/Publication dates, and alphabetize each of them. I have no idea how to alphabetize this sort of thing, I just know an array is needed, any help would be appreciated.

I know there are other kinks in this given code, which this is still a WIP. If anyone would point out those little bugs that would be nice, but not needed ^_^

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
string bookAuthTitle[20][3];
	if(catalog.is_open())
	{
		
		cout << "Welcome to the Book Store Catalog" << endl;
		cout << "What would you like to do today?" << endl;
		cout << "1. Input books to our database" << endl;
		cout << "2. Display Books from our database" << endl;
		cin >> choice;
		
		switch(choice)
		{
			case 1:
			{
				for(int x = 0; x <20; x++)
				{
	cout << "Enter the Books Author like this: (last name, first)" << endl;
					
                                        getline(cin, authorInput);
					cin.ignore(1);
					bookAuthTitle[x][1] = authorInput;
					cout << "Enter the Books Title" << endl;
					getline(cin, titleInput);
					cin.ignore(1);

if(titleInput.substr(0,4) == "the " || titleInput.substr(0,4) == "The ")
					{
fixed = titleInput.substr(4,10000000) + ", " + titleInput.substr(0,4);

					bookAuthTitle[x][2] = fixed;
					}
					else
					{
					bookAuthTitle[x][2] = titleInput;
					}
					
		cout << "Enter the Books Publication date" << endl;

					getline(cin, pubDateInput);
					//cin.ignore(1);
					bookAuthTitle[x][3] = pubDateInput;
					
					cout << bookAuthTitle[x][1] << '#' << bookAuthTitle[x][2] << '#' << bookAuthTitle[x][3] << endl;
					
					catalog << bookAuthTitle[x][1] << '#' << bookAuthTitle[x][2] << '#' << bookAuthTitle[x][3] << endl;
					
				}
			}
Hi,

I don't know if I understand your question correctly... Are you trying to develop a mini archive for a set of books?

If this is the case, then I would sugget proceeding as follows:
- write a class called Book which has some fields such as "Author", "Title", "Date", ...
- create a container of Book objects, like an array, a set, a list, a vector, ...
- put your books in the container
- sort the container by using one of the many exsisting sorting algorithms, like bubble sort, selection sort, insertion sort, quick sort, ...

Let me know if you have other doubts or problems.
What's your final output going to look like?
Because if it's Author/Title/Publication date then you can just alphabatize the author by last name.
Here's the function for that using bubblesort.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void bubblesort(string list[],int count)
// Sort an array of strings into descending order.
// Parameters:
// list – array of strings to be sorted
// count – (integer) number of values in the array
// Value passed back: sorted list
{
 int i; int j;
 string temp;
 for (i=0; i < n-1; i++)
 for (j=0; j < n-(i+1); j++)
 if (list[j] > list[j+1])
 {
 temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
 }
} 


How this works:
Sorting – rearranging a collection of values into a logical order
sorting orders
 ascending – smallest to largest (for strings, alphabetical order)
 descending – largest to smallest
Bubblesort sorting strategy
 decide on desired sorting order (ascending or descending)
 perform the following
1) move through the list comparing adjacent values
2) if adjacent values are out of desired order, interchange them
3) when a complete pass through the list has been made, the smallest
(largest) value will have "bubbled" to its proper location
4) repeat steps 1 and 3 ignoring the already sorted values (requires length-1
passes through the list)
Topic archived. No new replies allowed.