pass array of structs to a Bubble Sort

Hello Everyone.
I am struggling with passing an array of structs to a bubble sort. I want to sort on num in the struct but I am getting errors. I think I am close to the finish but just can't get there. If anyone has encountered this problem please help. Thanks. Full code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "stdio.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

struct emp
{
int num ;
string name ;
string age ;
string bs ;
} ;

void bubbleSort(emp list[], int length);

int main( )
{
ifstream fp;
    string Tmp;
fp.open("menu.txt");


emp emp_array[4];

if (fp)
{
    for (int counter = 0; counter < 4; counter++)
    {
        fp >> emp_array[counter].num;
        fp >> emp_array[counter].name;
        fp >> emp_array[counter].age;
        fp >> emp_array[counter].bs;
    }
}
else
    puts("erro");

bubbleSort(emp_array, 10);
                
for (int counter = 0; counter < 4; counter++)
{
    cout << emp_array[counter].num << "\t";
    cout << emp_array[counter].name << "\t";
    cout << emp_array[counter].age << "\t";
    cout << emp_array[counter].bs << endl;
}
}

void bubbleSort(emp list[], int length)
{
    emp temp;
    int iteration;
    int index;
    for (iteration = 1; iteration < length; iteration++)
    {
        for (index = 0; index < length - iteration; index++)
            if (list[index] > list[index + 1])
            {
                temp = list[index];
                list[index] = list[index + 1];
                list[index + 1] = temp;
            }
    }
}
list[index] > list[index + 1] Type of list[something] is emp. The compiler has no idea what you want to do with operator > were both arguments are emps. Either overload the operator or simply change that line to list[index].num > list[index + 1].num.

By the way, bubble sort algorithm is supposed to stop when the list is sorted. As you have it now, I'm not sure if that would work in all cases (try sorting a reverse sorted list to check)
Thanks hamsterman, it works perfectly. I have attached the full working routine for those who might have the same problem and need assistance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "stdio.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

struct emp
{
int num ;
string name ;
string age ;
string bs ;
} ;

void bubbleSort(emp list[], int length);

int main( )
{
ifstream fp;
    string Tmp;
fp.open("menu.txt");


emp emp_array[4];

if (fp)
{
    for (int counter = 0; counter < 4; counter++)
    {
        fp >> emp_array[counter].num;
        fp >> emp_array[counter].name;
        fp >> emp_array[counter].age;
        fp >> emp_array[counter].bs;
    }
}
else
    puts("erro");

bubbleSort(emp_array, 4);
                
for (int counter = 0; counter < 4; counter++)
{
    cout << emp_array[counter].num << "\t";
    cout << emp_array[counter].name << "\t";
    cout << emp_array[counter].age << "\t";
    cout << emp_array[counter].bs << endl;
}
}

void bubbleSort(emp list[], int length)
{
    emp temp;
    int iteration;
    int index;
    for (iteration = 1; iteration < length; iteration++)
    {
        for (index = 0; index < length - iteration; index++)
             if (list[index].num > list[index + 1].num)
            {
                temp = list[index];
                list[index] = list[index + 1];
                list[index + 1] = temp;
            }
    }
}
Topic archived. No new replies allowed.