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

  • Thread starter Thread starter z-component
  • Start date Start date
  • Tags Tags
    C++ Sorting
Click For Summary
The discussion revolves around a C++ programming issue related to sorting an array of student structures by last name. The user encounters errors in their sorting algorithm, particularly in the last for loop. Key points include the identification of a critical error preventing the program from running, specifically the incorrect assignment of a string variable to a student structure. The user seeks assistance to resolve these issues and successfully implement the sorting functionality. Ultimately, another participant confirms they managed to fix the problem, indicating a resolution was reached.
z-component
Messages
494
Reaction score
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
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.
 
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.
 
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:
I got it to work. Thanks everyone.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 30 ·
2
Replies
30
Views
4K
  • · Replies 75 ·
3
Replies
75
Views
6K
Replies
20
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K