struct array and selection sort

I need to write code that defines a struct, an array that can hold 20 occurrences of the struct and a print function.
Read in data
display
sort by name using selection sort
display.

I got up to the first display down then added selection sort to sort
by name and got the error code

Error C2676 binary '>': 'person_info' does not define this operator or a conversion to a type acceptable to the predefined operator

Here is my lines 58, 59, and 60 the > on line 59 is the issue at the moment
for (x = start + 1; x < MAX_PERSON; x++)
if (per_arr[smallest] > per_arr[x])
smallest = x;

Thanks for any help





#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
using namespace std;

const int MAX_PERSON = 20;

struct person_info
{
string name;
int age;
float iq;
};

void print(person_info[], int num_pers);
void sel_sort(person_info per_arr[], int start, int MAX_PERSON);
void swap(int&a, int&b);
int main()
{
ifstream in_file;
person_info rec_arr[MAX_PERSON];
int num_ele = 0, smallest, temp;
in_file.open("data.txt");

in_file >> rec_arr[num_ele].name;

while (in_file && num_ele < MAX_PERSON)
{
in_file >> rec_arr[num_ele].age
>> rec_arr[num_ele].iq;
num_ele++;
if (num_ele < MAX_PERSON)
in_file >> rec_arr[num_ele].name;
}
print(rec_arr, num_ele);

sel_sort(rec_arr, 0, MAX_PERSON);

print(rec_arr, num_ele);

return 0;
}

void print(person_info per_arr[], int num_pers)
{
for (int count = 0; count < num_pers; count++)
cout << setw(8) << per_arr[count].name
<< setw(12) << per_arr[count].age
<< setw(12) << per_arr[count].iq << endl;
}

void sel_sort(person_info per_arr[], int start, int MAX_PERSON)
{
int smallest, x;
smallest = start;
for (x = start + 1; x < MAX_PERSON; x++)
if (per_arr[smallest] > per_arr[x])
smallest = x;
swap(per_arr[smallest], per_arr[start]);

if (start < MAX_PERSON - 2)
sel_sort(per_arr, start + 1, MAX_PERSON);
}

void swap(int&a, int&b)
{
int temp = a;
a = b;
b = temp;
}
Last edited on
Either you provide an operator>(const person_info &pi) const in your class person_info or you use the member variable(s) directly that should be used for the comparison.
Topic archived. No new replies allowed.