Sorting mod

Pages: 12
I have sort function that was originally for strings. I'm using a structure. I was trying to modify it so it sorted the last name of the structures in alphabetical order. Got a little wild. The compiler is on the verge of leaving me for a new programmer ...

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
//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 (infoType.lname[j] != "" && infoType.lname[smallest] != "")
			{
				info1 = infoType.lname[j];
				info2 = InfoType.lname[smallest];
				info1[0] = tolower(info1.at(0)); 
				info2[0] = tolower(info2.at(0));
				if (info1 < info2)
				{
					smallest = j;
				}
			}
		}
 // This line below is a horrible attempt to modifiy
		info1[i].fname.swap(info2[smallest]);
	}





}

Most of my misunderstanding comes from types I believe judging by the errors on the compiler.
If you would be so kind as to add a little explanation I would appreciate it.
It would be helpful if you would actually show us those compiler errors.
OK,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
1>  newnew.cpp
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(234): error C2059: syntax error : '.'
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(235): error C2143: syntax error : missing ';' before '{'
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(236): warning C4832: token '.' is illegal after UDT 'infoType'
1>          c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(11) : see declaration of 'infoType'
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(236): error C2275: 'infoType' : illegal use of this type as an expression
1>          c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(11) : see declaration of 'infoType'
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(236): error C2228: left of '.lname' must have class/struct/union
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(237): error C2065: 'InfoType' : undeclared identifier
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(237): error C2228: left of '.lname' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(247): error C2228: left of '.fname' must have class/struct/union
1>          type is 'char'
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(247): error C2228: left of '.swap' must have class/struct/union
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========




Ah, right.

Well, the problem is pretty clearly explained in the compiler errors. You're trying to use infoType as an object name, but you've defined it as a type.

Presumably, what you meant to do was use one of the members of the tele array?

Compiler errors can sometimes be cryptic and misleading, but usually they'll be a very good indication of what's wrong. And they even tell you which line number the problem is at.
Presumably, what you meant to do was use one of the members of the tele array?

Yeah I'm trying to sort by last name. How is this done? And what do you mean by object name?
Thanks,
If you don't understand the difference between an object and a type, then you're going to be in all sorts of trouble. It's absolutely fundamental to programming in C++, or in any typed language.

In a nutshell, consider the following statement:

 
int i = 0;


i is on object. It corresponds to a particular piece of memory, containing specific values.
int is a type. It tells us something about how the values stored in memory are treated.

(Yes, it's unusual to consider a variable of a primitive type as an "object", but conceptually that's what they are.)

You can change the value of i - for example:

1
2
3
4
5
int i = 0;

i = 2;
i = 3 + 1;
i ++;


You cannot change the value of int, because int is not an object with a value. It is simply a type:

 
int = 1;  // THIS MAKES NO SENSE 


Nor can you read the value of int, because it has no value:

1
2
std::cout << "The value of int is " << int << std::cout; // THIS MAKES NO SENSE


Now consider:

1
2
int i = 0;
int j = 1;


i and j are two different objects. They correspond to different bits of memory, and hence can hold different values.

i and j have the same type. They are both int. They share the same behaviour, but hold different values.

Now consider the following, from your code:

infoType tele[]

Here, you are declaring an object. Its name is tele. Its type is an array of infoType objects - in other words, its type is a pointer to infoType, or infoType*.

Now look at your line:

 
if (infoType.lname[j] != "" && infoType.lname[smallest] != "")


infoType is a type. Semantically, it is the equivalent of int. It has no value. It cannot be array-indexed. It cannot be read. It cannot be set to a value.

The name of the object that you have passed into your sort function is tele. That is the thing you should be looking at the values of.
Last edited on
Thanks man,

I tired to clean this up. Still getting some errors. This is a small fraction of the list. :(

Build started: Project: newnew, Configuration: Debug Win32 ------
1> newnew.cpp
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(324): error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'infoType' (or there is no acceptable conversion)
1> could be 'built-in C++ operator!=(const char [1], const char [1])'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(425): or 'bool std::operator !=(const std::error_code &,const std::error_condition &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(432): or 'bool std::operator !=(const std::error_condition &,const std::error_code &)'
1> while trying to match the argument list '(infoType, const char [1])'
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(324): error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'infoType' (or there is no acceptable conversion)
1> could be 'built-in C++ operator!=(const char [1], const char [1])'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(425): or 'bool std::operator !=(const std::error_code &,const std::error_condition &)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(432): or 'bool std::operator !=(const std::error_condition &,const std::error_code &)'
1> while trying to match the argument list '(infoType, const char [1])'
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(326): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'infoType' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(707): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(std::basic_string<_Elem,_Traits,_Ax> &&)'
1> with


What I think it happening is that once again it is an issue with types.
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
//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]);
	}


What I still don't get is. I just want to sort the by last name so how do I compare first them when the last name belongs to infoType (my structure?).
Also If infoType is has variables which are strings why do I get errors if I say != or =.
I'm not understanding this.
You've created your own type, infoType. C++ therefore doesn't know how to compare the two objects. You need to define how to do it, by creating an equality operator for infoType.
Last edited on
We have not covered that at all. Can you please explain?
Also what do you think about fixes I made?
Thanks
To be honest, I'm sure if you Google "C++ equality operator", you'll find some pretty decent explanations.

Actually, I've just noticed that you're going to need an assignment operator, too.
Found this

http://www.cplusplus.com/reference/string/operators/
No clue how it helps this is getting a little beyond my course.

Well, you could try cracking open whatever textbook you're learning C++ from.

Or, y'know, if you're feeling adventurous, you could try looking at more than just the very first hit Google gives you. Or get creative with your Google searches.

Seriously, it's taken me more time to type this message than it would for you to look for an appropriate reference source.

Edit: Are you sure you want to compare the entire infoType object? Or is there a specific data member you just want to compare?
Last edited on
All I want to do is alphabetize the the infoType by last name. So when I display it in another function it's orderly and proper.
It wasn't the first hit.
I spent from the last time I was messaged on google.

MikeyBoy (71)

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, I don't really know how to solve this issue.
How do you define a operator for the structure?
Thanks
Hmm... as far as I know, a struct is the same as a class, the only difference being that in a struct, all members are public by default. I've never heard that you can't define arithmetic operators for a struct, but it might be a weird quirk of C++.

Actually, I think I misread the code first time around. You're not comparing two infoType objects - you're comparing an infoType object to a string. I assume you were intending to compare a member of the structure, i.e. something like this:

 
if (tele[j].lastName != "" && tele[smallest].lastname != "")


You need to get clear on the types of your objects. tele is an array of infoTypes, not an array of strings.
Last edited on
Right but it contains does contain string variables, so I though that I would be OK. I now have no clue what to do. I still need to display my tele[] in alphabetical order and no clue how to do it. I haven't learned classes yet.

Well, a struct is, effectively, a collection of other data members. Presumably one of those data members is the last name? Well, assuming that member is called lastName, then you access the last name of the i-th member of the array as:

tele[i].lastName

Does that make sense?

Edit: C++ can't magically know that, when you try and compare the two, what you really want is to compare the lastName member. You have to be specific.
Last edited on
Yeah but I can't compare them it seems because of the quote I posted in from my text earlier. So how does this work now?

I can try what you are saying but it still from what I understand doesn't account for how to deal with the relational operators issue. :(

Look again at your code, at the point where you're trying to do a comparison. It's line 15 in the snippet you posted. Right now, you're trying to compare the whole of a structure - tele[j] to a string - "". This makes no sense - you need to compare a string to a string.

Instead of comparing the whole struct to "", you need to compare just the string member you're interested in. Again - C++ can't magically know that when you type tele[j], you're only interested in the last name. You have to be specific.
Last edited on
Mikey we now have issue with the swap.

This is what I got back
1>------ Build started: Project: newnew, Configuration: Debug Win32 ------
1> newnew.cpp
1>c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(343): error C2039: 'swap' : is not a member of 'infoType'
1> c:\users\jordan\documents\visual studio 2010\projects\whatshesent\whatshesent\newnew.cpp(15) : see declaration of 'infoType'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This is what I did

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]);
	}

}

OK Mikey let me ask you this.
Remeber when I said relational operators don't apply to structs? Well, my question is do you think that operations that are defined for a string will still hold if you are specific like I tried to be above? Because if you think about it...don't you have a struct (type) and contained in that is a string(in this case) which is another (type,.... I think) so the operations that I'm after are defined for that type or do you think that because it is within a struct it doesn't matter and that the struct is voids the operations I 'm after because the string is contained within a struct??
Hope that makes sense.
Pages: 12