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

Discussion Overview

The discussion revolves around a C++ programming problem related to sorting an array of student structures by last names in alphabetical order. Participants explore issues with the implementation of a sorting algorithm, specifically focusing on errors encountered during compilation and execution.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an attempt to sort an array of student structures but encounters errors in the last for loop.
  • Another participant questions whether the errors are due to the algorithm not sorting correctly or if the program fails to run altogether.
  • A participant notes an "extremely elementary error" that should be visible in a good IDE but does not specify what it is.
  • There is a suggestion regarding the swapping of student structures, questioning if the participant wants to perform that operation.
  • A later reply indicates that the original poster managed to resolve the issue and get the program to work.

Areas of Agreement / Disagreement

The discussion includes multiple viewpoints regarding the nature of the errors and the effectiveness of the sorting algorithm. While one participant claims to have resolved the issue, the specifics of the errors and their solutions remain unclear, indicating that some aspects of the discussion are unresolved.

Contextual Notes

Participants do not clarify the exact nature of the errors encountered, nor do they provide a complete resolution to the sorting logic. The discussion lacks details on the assumptions made regarding the input data and the expected output.

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
7K
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
3K
  • · 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