- #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?
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;
}