Solving a C++ Problem: Sorting Last Names in Alphabetical Order

  • C/C++
  • Thread starter z-component
  • Start date
  • Tags
    C++ Sorting
In summary, the conversation was about a C++ problem where the goal was to sort a structure of 4 elements containing an array of items in alphabetical, ascending order. The person kept getting errors while trying to implement the sorting algorithm, and others suggested looking for an elementary error and fixing it. Eventually, the person was able to solve the problem with the help of others.
  • #1
z-component
494
2
[SOLVED] C++ problem

I'm trying to make a structure of 4 elements to store an array of items. I'm trying to sort the lastName element in alphabetical, ascending order, but I keep getting errors around the last for loop. Can someone tell me what I'm doing wrong?


Code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct student {
	string firstName;
	string lastName;
	double GPA;
	int studentID;
};

int main()
{
	student s[10];

	ifstream in("people.txt");
	int numStudents = 0;

	const int size = 7;

	bool flag = true;
	for (int endIndex = size - 1; endIndex > 1 && flag; endIndex--) {
		flag = false;
		for (int startIndex = 0; startIndex < endIndex; startIndex++) {
			if (s[startIndex + 1].lastName < s[startIndex].lastName) {
				// adjacent elements in the array are out of order - swap them
				string temp = s[startIndex];
				s[startIndex] = s[startIndex + 1];
				s[startIndex + 1] = temp;
				flag = true; // set flag to show we have to continue...
				
			}
	}
	return 0;
}
 
Technology news on Phys.org
  • #2
You keep getting errors? Does that mean you have run the algorithm and it doesn't sort the list correctly, or does it mean that it doesn't run?

I can see an extremely elementary error, any good IDE would show it.
 
  • #3
The program doesn't run at all. What is the error you found? Sometimes I got it to stop erring but then whenever I tried outputting I got more errors.
 
  • #4
z-component said:
Code:
[b][u]student[/u][/b] s[10];

[...]

[b][u]string[/u][/b] temp = s[startIndex];
s[startIndex] = s[startIndex + 1];
s[startIndex + 1] = temp;
}

Can you do this? Do you want to?
 
Last edited:
  • #5
I got it to work. Thanks everyone.
 

1. How do you sort last names in alphabetical order using C++?

To sort last names in alphabetical order using C++, you can use the built-in sort function from the algorithm library. This function takes in two parameters: the beginning and end iterators of the list of last names. It will then rearrange the elements in the list to be in alphabetical order.

2. Can you explain the syntax for sorting last names in C++?

The syntax for sorting last names in C++ using the sort function is as follows: sort(lastNames.begin(), lastNames.end()); where lastNames is the name of the list or vector containing the last names. This syntax will sort the elements in the list in ascending order by default.

3. What if I want to sort last names in descending order?

To sort last names in descending order, you can use the greater function from the functional library as the third parameter in the sort function. This will reverse the default sorting order and sort the elements from highest to lowest.

4. How do I handle special characters or accented letters when sorting last names in C++?

C++ has a built-in function called locale that can be used to handle special characters and accented letters when sorting. You can pass in a specific locale as the fourth parameter in the sort function to specify the language and sorting rules for your list of last names.

5. Is there a more efficient way to sort last names in C++?

Yes, there are other sorting algorithms that can be more efficient for specific situations, such as the quick sort or merge sort algorithms. However, the sort function in C++ is generally efficient and suitable for most cases of sorting last names in alphabetical order.

Similar threads

  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
2
Views
930
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
12
Views
15K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top