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

  • Context: C/C++ 
  • Thread starter Thread starter z-component
  • Start date Start date
  • Tags Tags
    C++ Sorting
Click For Summary
SUMMARY

The forum discussion addresses a C++ programming issue related to sorting an array of student structures by last names in alphabetical order. The user encountered errors in their sorting algorithm, specifically within the nested for loop. Key issues identified include incorrect handling of string assignments and the need for proper array indexing. The solution involves correcting the swapping mechanism to ensure that the student structures are properly sorted without runtime errors.

PREREQUISITES
  • Understanding of C++ structures and arrays
  • Familiarity with string manipulation in C++
  • Knowledge of sorting algorithms, particularly bubble sort
  • Experience with file input/output in C++
NEXT STEPS
  • Review C++ string handling and assignment techniques
  • Learn about bubble sort implementation in C++
  • Explore debugging techniques in C++ IDEs
  • Study file I/O operations in C++ for reading data into structures
USEFUL FOR

C++ programmers, computer science students, and anyone interested in learning about sorting algorithms and data structure manipulation in C++.

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.
 

Similar threads

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