Trouble with struct and bubble sort

I'm not really sure what I'm doing wrong, but I think it's a simple mistake.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*****************************
 *  Made by Me               *
 *  Assignment 6             *
 *  ece270                   *
 *****************************/

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

struct Node
{
	char name[20];
	int ID;
	int dist;
};
static const Node default_node = {"Mr. Nobody", 666, -1};
	
void READIN(Node[],string);
void READOUT(Node[],int);
void MAXDIST(Node[],int);
void BSORT(Node[],int);

int main()
{
	string in_file = "a6.txt";
	Node NA[10];

	READIN(NA,in_file);
	READOUT(NA,10);
	MAXDIST(NA,10);
	BSORT(NA,10);

	return 0;
}

void READIN(Node NA[], string in_file)
{
	ifstream in_data;
	int i = 0;

	in_data.open(in_file.c_str());

	while (!in_data.eof())
	{
	in_data >> NA[i].name;
	in_data >> NA[i].ID;
	in_data >> NA[i].dist;
	i++;
	}
	in_data.close();

}

void READOUT(Node NA[], int count)
{
	for(int i=0; i<count; i++)
	{
		cout << NA[i].name << "\n";
		cout << NA[i].ID << "\n";
		cout << NA[i].dist << "\n" << "\n";
	}

}

void MAXDIST(Node NA[], int count)
{
	int maxNodeNum;
	int maxDist = 0;

	for(int i=0; i<count; i++)
	{
		if (NA[i].dist > maxDist)
		{
			maxNodeNum = i;
			maxDist = NA[i].dist;
		}
	}

	cout << NA[maxNodeNum].name << "\n";
	cout << NA[maxNodeNum].ID << "\n";
	cout << NA[maxNodeNum].dist << "\n" << "\n";

}

void BSORT(int NA[],int count)
{

	for(int i=count; i>0; i--)
	{
		for(int j=0; j<i-1; j++)
		{
			if ( NA[j] > NA[j+1] )
			{
				Node temp = default_node;
				temp = NA[j+1];
				NA[j+1] = NA[j];
				NA[j] = temp;
			}
		}
	}

	READOUT(NA,count);
}
Input file (I think the forum added extra lines):
April,Joe

3120

12



Matthews,Jocob

4592

39



Garfield,Kitty

8917

33



Lake,Bill

2233

21



Jones,Betty

8912

18



Goodman,Betty

4598

19



Lands,Norman

7812

36



Johnson,Carl

8917

29
Error:
me@ume:~/code$ g++ struct_test.cpp -o struct_test.out
struct_test.cpp: In function ‘void BSORT(int*, int)’:
struct_test.cpp:97: error: no match for ‘operator=’ in ‘temp = *(NA + ((((long unsigned int)((long unsigned int)j)) + 1u) * 4u))’
struct_test.cpp:13: note: candidates are: Node& Node::operator=(const Node&)
struct_test.cpp:99: error: cannot convert ‘Node’ to ‘int’ in assignment
struct_test.cpp:104: error: cannot convert ‘int*’ to ‘Node*’ for argument ‘1’ to ‘void READOUT(Node*, int)’


Thank you for our help.
I think there's something amiss between on lines 23 and 87.
Last edited on
closed account (Lv0f92yv)
Indeed there is. I have made this type of mistake before, and it seems the compiler does a really poor job of describing the problem... I'm sure there is a reason for it, but it can be quite frustrating if your nor used to seeing the error.
*facepalm*

Thanks, but after I changed 87 to void BSORT(Node NA[],int count) I still get this error.
me@ume:~/code$ g++ ass6.cpp -o ass6.out
struct_test.cpp: In function ‘void BSORT(Node*, int)’:
struct_test:94: error: no match for ‘operator>’ in ‘*(NA + ((long unsigned int)(((long unsigned int)j) * 28ul))) > *(NA + ((((long unsigned int)((long unsigned int)j)) + 1u) * 28u))

It means that Node doesn't have a > operator defined for it.

Btw, how the heck did you manage to get that kind of error text? I've never seen g++ do that kind of stuff.
Thanks again ^^;

All I had to do is change it from if ( NA[j] > NA[j+1] ) to if ( NA[j].dist > NA[j+1].dist ).

firedraco wrote:
Btw, how the heck did you manage to get that kind of error text? I've never seen g++ do that kind of stuff.
Well... you can try compiling my code.
Topic archived. No new replies allowed.