Hey guys, I'm finishing up my final project and I'm running into some problems. The project is: Create 5 myFloat objects and 5 myName objects, print them to the output screen, then pass them through a bubble sort template to arrange them into numerical order based on x, and alphabetical order based on last name.
I have a template that works if I assign a random array of numbers and chars into it, but I'm stuck on how to pass the myFloat and myName objects into it to be sorted. Any guidance here to point me at what I need to correct would be awesome. Here's my current code:
Ya, I can create an array and pass it through the bubble sort but that doesn't do me any good. I need to sort them based on what x is and based on what the last name is. I hate being so close, and yet so far.
operator > got overloaded in both classes to compare objects in the order of x for myFloat and in the order of last name, then first name for myName. I'm just lost on how to incorporate them into the bubble sort
I've gone back to what my original code was in the first post. I've been reading up on bubble sort templates and I'm still as lost as I was hours ago. I know the template means you can re-use the code with different types, I'm just not sure how to implement it so I can use it to sort the x's in numerical order, and last names in alphabetical order.
Could I just put f1, f2, f3, f4, f5 into an array after creating them and then sort that? Or am I just talking out of my butt now? If anybody could explain to me a little more how I'd go about making this work that would be phenomenal!
well, in the case you wish to use it in a template, you need to overload the > and < and == operators for your class.
In this case i think someone already helped you do the overloading in a diferent thread.
As for the bubble sort for you just compare them with ">" and "<" and swap them if necessary.
As for the overloading you overloaded them wrong.
You only need to return true if the sign is corect(">" or "<").
For exemple this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
bool myName::operator>(myName m)
{
if ( fn > m.ln )
{
returntrue;
}
if ( fn == m.fn || ln <= m.ln)
{
returntrue;
}
else
{
returnfalse;
}
}
should be like this
1 2 3 4 5 6 7 8 9 10 11
bool myName::operator>(myName m)
{
if ( fn > m.ln )
{
returntrue;
}
else
{
returnfalse;
}
}
And the same with myFloat? Also, this is the only thread I've made on this, there is a similar one which only sorts the numbers for myFloat, not last names like in my case.
// ...
bool myFloat::operator>(myFloat m)
{
if ( x > m.x )
{
returntrue;
}
else
{
returnfalse;
}
}
bool myFloat::operator<(myFloat m)
{
if ( x < m.x)
{
returntrue;
}
else
{
returnfalse;
}
}
bool myFloat::operator==(myFloat m)
{
if ( x == m.x)
{
returntrue;
}
else
{
returnfalse;
}
}
Like this?
Also, I keep getting errors when I try to put my values for n1, n2, etc into the bubble sort and I also keep getting "no instance of function template bubblesort matches the argument list". And "no suitable conversion function from myFloat to float exists". This sort template is kicking my ass :(
Ok solved:)
For the template to work you need to use c style arrays
Meaning something like this:
1 2
myFloat flt[x]; // x number of items, or
myFloat *pFlt = new myFloat[x]; // x number of items
It works the same for myName class;
I will post the whole code since the thing i copyied from here didn't compile due to some missing stuff :)
Compare with what you had and see what was wrong.
Also keep in mind that for strings it's better to use the string member function compare, rather the ">", "<", "==" signs :). I did that in the myName class;
check -> http://www.cplusplus.com/reference/string/string/compare/ for more info on string::compare;
I added a little bonus :P
main.cpp
#include <iomanip>
#include "myFloat.h"
myName::myName()
{
}
myName::myName(string f , string l)
{
fn = f;
ln = l;
}
void myName::setF(string f)
{
fn = f;
}
void myName::setL(string l)
{
ln = l;
}
string myName::getF()
{
return (fn);
}
string myName::getL()
{
return (ln);
}
ostream & operator<< (ostream & create, myName obj)
{
create << "Name is: " << obj.getF() << " " << obj.getL() << endl;
return create;
}
// you use the compare function from std::string
// because using just > < = to compare strings doesn't work so well
bool myName::operator>(myName m)
{
if ( (fn.compare(m.fn) > 0))
{
returntrue;
}
else
{
returnfalse;
}
}
// you use the compare function from std::string
// because using just > < = to compare strings doesn't work so well
bool myName::operator<(myName m)
{
if(fn.compare(m.fn) < 0)
{
returntrue;
}
else
{
returnfalse;
}
}
// added this
bool myName::operator==(myName m)
{
// you use the compare function from std::string
// because using just > < = to compare strings doesn't work so well
if(fn.compare(m.fn) == 0)
{
returntrue;
}
else
{
returnfalse;
}
}
Adding the do-while loop and using a bool in the bubble sort really cleared things up for me. I also changed things up a little bit since your code there compares the first names, I just modified it to compare and swap out the last names.
Thanks a lot nedo, you were a huge help. Kudos to you!