IMPOSSIBLE?

Nov 23, 2012 at 11:00pm
I have a phone directory program. I need to display the directory by last name in alphabetical order. I have used an array called tele this array is of the type infoType.

Below is the sort algorithm I tried to use. When I tried to use the relation operators on the structure I got all sorts of errors. Come to find out ...



"The only built- in operations on a struct are the assignment and member access operations."


"Neither arithmetic nor relations operations are allowed on struct(s)"


That's what my lovely reference says.

C++ Programming from Problem Analysis to Program Design
D.S Malik

So how can I do this without getting so complicated if it can be done. I know that the sort function is probably out of sorts but the framework is there this is what I have as of now.

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
//Sort Function
void sort(infoType tele[], const int& size)
{
	int i = 0;
	int j = 0;
	int smallest;
	string info1 = "", info2 = "";
 
	for (i = 0; i < size - 1; i++)
	{
		smallest = i;
 
		for (j = i + 1; j < size; j++)
		{
			if (tele[j] != "" && tele[smallest] != "")
			{
				info1 = tele[j];
				info2 = tele[smallest];
				info1[0] = tolower(info1.at(0)); 
				info2[0] = tolower(info2.at(0));
				if (info1 < info2)
				{
					smallest = j;
				}
			}
		}
 
		tele[i].swap(tele[smallest]);
	}

}


Hopefully someone know a good way around all this.
Thanks
Nov 23, 2012 at 11:13pm
You need to either define operator< for your class or provide a comparison function and then call std::sort.

"Neither arithmetic nor relations operations are allowed on struct(s)"

That is incorrect.
Nov 23, 2012 at 11:33pm
Can you explain more I don't really know anything you just said.. sorry.

That is incorrect.
Really? Not that I don't belive you but you should write the author of my book and tell him to quit selling bull*hit lol :)

That's curious thanks for the info ...can you elaborate if it doesn't make you want pull your hair out.

I changed my function to try and make the compairsons more specific what I had above doesn't make sense

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
//Sort Function
void sort(infoType tele[], const int& size)
{
	int i = 0;
	int j = 0;
	int smallest;
	string info1 = "", info2 = "";
 
	for (i = 0; i < size - 1; i++)
	{
		smallest = i;
 
		for (j = i + 1; j < size; j++)
		{
			if (tele[j].lname != "" && tele[smallest].lname != "")
			{
				info1 = tele[j].lname;
				info2 = tele[smallest].lname;
				info1[0] = tolower(info1.at(0)); 
				info2[0] = tolower(info2.at(0));
				if (info1 < info2)
				{
					smallest = j;
				}
			}
		}
 
		tele[i].swap(tele[smallest]);
	}

}
Nov 24, 2012 at 12:53pm
It works like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <algorithm>

struct InfoType
{
    std::string lname;
    bool operator<(const InfoType& rhs) const {return lname<rhs.lname;}
};

void sort(InfoType tele[], size_t size)
{
    std::sort(tele,tele+size);
}

As you can see, there's no point in having the sort function, so you can just call std::sort directly instead.

Really? Not that I don't belive you but you should write the author of my book and tell him to quit selling bull*hit lol :)

That makes no sense without knowing the full context and it's not worth bothering either. Books about programming (C++ in particular) being of poor quality are the rule rather than the exception.
Last edited on Nov 24, 2012 at 12:56pm
Nov 24, 2012 at 3:15pm
Thanks dude. And that's pretty funny. It's like my math book.
Topic archived. No new replies allowed.