# How come this is happening?

• 03-21-2009
Angermanaged
How come this is happening?
I am trying to rewrite the string class and i am having a little trouble overloading all the rational operators. Here is the code i have for the less than operator.

length is an integer that represents the length of the string object.

first is a pointer to the first character of the string.

Code:

```bool String::operator< (const String& a) const {    int temp = difference(a);   if(temp == length && length < a.length)  //Testing to see if the leftString is a prefix of rightString       return true;   if(temp == a.length && a.length < length)       return false;   if((int)*(first+temp) < (int)*(a.first+temp))       return true;   else       return false; }```
Can anyone explain to me why this function is returning:

Comparing "part" and "particular"
< False
<= False
> False
>= False
== False
!= True

When clearly part is a prefix of particular

difference() returns the index of where the two strings differ.

Code:

```int String::difference(String a) const {   int placeholder = a.length+1;       for(int x = 0; x < a.length; x++)   {       if(first+placeholder != a.first+placeholder)           {  if(placeholder > x)                     placeholder = x;           }   }   return placeholder; }```
Thank you all who ltry to help me with this problem.
• 03-21-2009
Daved
What is placeholder used for in the difference function? If you're comparing part and particular, it starts off as 11. Then your first iteration through the loop has:
Code:

`if(first+11 != a.first+11)`
That can't be right. Note that it will always be the same every time through the loop, and if you're trying to use it as an index it will always be too big.

I'd look at that function and try to fix it. Test it separately from operator< first and when it works then move on to trying to get your operator< working.
• 03-21-2009
tabstop
I can't imagine how your difference function gives you the right answer. Does it give you the right answer? I'm pretty sure you're always going to get the first index where they match, which is not the same thing.
• 03-21-2009
Angermanaged
thank
thank you i will look into rewriting it