I posted this question in beginners someone had this to reply
1 2 3 4 5
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.
It would be necessary to write an overload for operator < if you were going to use the std::sort() to sort your array of structures.
However, you have written your own sort routine, custom for sorting infoType objects. You may define what < means there.
Eg;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
for (i = 0; i < size - 1; i++)
{
smallest = i;
for (j = i + 1; j < size; j++)
{
// this condition defines what < means to infoTypes
if (tele[j].lname < tele[smallest].lname )// compare last names
{
infoType temp = tele[j];// here's the swap
tele[j] = tele[smallest];
tele[smallest] = temp;
}
}
// tele[i].swap(tele[smallest]); <-- you were not far off here
}
You got close with that swap() call! It's in the wrong place though.
If you #include<algorithm> then that would be swap( tele[j], tele[smallest] );
You have things very confused here. Hope this helps out.
I'm very confused.
Can you please clear this up for me.
I have used your algorithm. I did not get any errors. But I have not put it to use yet because I taking 5 minutes from something else to see what the info was on here.
Anyways
How does this define < ? Can you explain.
1 2
// this condition defines what < means to infoTypes
if (tele[j].lname < tele[smallest].lname )// compare last names
tele[i].swap(tele[smallest]); what the heck is this? This was an attempt to swap the 2 I thought this was a function. My lab aid used this for a sorting just strings so I thought that I could modify it ...
The confusion is that I read in my book that ....
"Neither arithmetic nor relations operations are allowed on struct(s)"
So I started falling down the rabbit hole. I thought that is why I was getting some of the errors I posted earlier. Because I couldn't do != and == on infoType.
Really a lot of the spiral into the confusion was because of this and I'm not so good at programming haha. But can you please explain this to me. I don't have any grasp on my question I posted above regarding <
THANKS! and is the error not all control paths return a value something to worry about ? ha
The one you suggested works fine. I will give you the other one. But you have to tell me why I don't need any of that crap. Like I said my text said I couldn't use the things. I know the alphabet ha. But why on earth did everyone tell me all this mumbo jumbo? and you say "ahh no its fine" You have to explain please I won't sleep tonight.
This is the other function. My lab aid got it off google I suppose. Since my teacher says we do not have to reinvent the wheel. YOU DO NOT HAVE TO DEFINE ANY OPERATORS OR OTHER FUNCTIONS
WHY!!!...no but seriously please explain...this way I can actually take something from this mess.Thansk
But you didn't explain ... how come even my book said the the realational operators dont' work for structs and then you say it does? Why is that ...Thx
Come on dude you got to explain please. Driving me nuts
But you didn't explain ... how come even my book said the the realational operators dont' work for structs and then you say it does? Why is that ...Thx
I imagine what your book said is that the relational operators are not overloaded for types you create by default. You can, however, create your own if you desire to. On the other hand, the overloaded operators for types like std::string don't stop working if you put objects of those types into a struct. You can still compare the members directly without defining overloaded operators for the struct/class.
Does the new sort function work or not?
EDIT: I tested the new function and it does not work.
I am not going to troubleshoot your sort function for you.
Post back with a known to be working sort function and I will try again.
EDIT2: It is definitely the selection_sort() you gave.
I adapted a bubbleSort() I have and it worked fine.
All I did was put it in my program It's just like hanging out because I haven't called my sort function from the function I want to use. I have been doing other homework sorry. I don't understand this ..
1 2 3 4 5 6 7
EDIT: I tested the new function and it does not work.
I am not going to troubleshoot your sort function for you.
Post back with a known to be working sort function and I will try again.
EDIT2: It is definitely the selection_sort() you gave.
I adapted a bubbleSort() I have and it worked fine.
Are you talking about the function that you just gave me doesn't work so you have created a new one? The function that I gave you was the one I was trying to modify that function is correct as far as I know. The one I gave you for strings. I feel slow I don't know what your telling me here. I have this section to finish then I will work on programming more. Thanks more helping me.
And thank you cire.
So the operations that are defined for string are not void all of a sudden because they are within a struct. The same as all the other types like int and char...? Thank you thank you!
void selectionSort(infoType tele[], int size)
{
int i, j;
int smallest;
for (i = 0; i < size - 1; i++)
{
smallest = i;
for (j = i + 1; j < size; j++)
{
if (tele[j].lname != "" && tele[smallest].lname != "")
{
// if (Word1 < Word2)// old rule
if( tele[j].lname < tele[smallest].lname )// new rule
{
smallest = j;
cout << "smallest = " << smallest << endl;
}
}
}
infoType temp = tele[smallest];// here's the swap
tele[smallest] = tele[i];// in last version j was here
tele[i] = temp;// instead of i
}
}
I see I see. I was thinking I can't see why it was wrong because I used the one I just gave in my last homework. I'm almost done with this other homework then I will program for a while. Thank you for your help.
You're welcome. Did you notice cires post above?
As he pointed out we are not applying any relational operator to an infoType here.
We are applying the comparison operator < in just one place, on line 16 above where it is applied between 2 strings. We are not comparing whole infoTypes here.
Here's a comparison:
1 2 3
if( tele[i] < tele[j] ) // here two infoItems are being compared. We can't do this.
if( tele[i].lname < tele[j].lname ) // here two strings are being compared. No problem.
Yah it really does. Dude I'm almost done with my program I stayed up late and it was working. Now I'm having an issue when inside my program. When I'm done with a function it is supposed to return to the menu. But all that happens is the program stops. Here is the link http://www.cplusplus.com/forum/general/85806/#msg460498
I just need to format and figure out why it isn't continuing to work. When it was working.