How come this is happening?

This is a discussion on How come this is happening? within the C++ Programming forums, part of the General Programming Boards category; I am trying to rewrite the string class and i am having a little trouble overloading all the rational operators. ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    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.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    thank

    thank you i will look into rewriting it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is happening here PLZ help me
    By jayee_spicyguy in forum C Programming
    Replies: 1
    Last Post: 11-15-2008, 05:47 AM
  2. Explain me what is happening
    By capvirgo in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 07:26 AM
  3. Weird things happening!!!!!!
    By korbitz in forum Windows Programming
    Replies: 4
    Last Post: 03-22-2004, 05:31 AM
  4. Funny things happening to values outside of function
    By drb2k2 in forum Windows Programming
    Replies: 2
    Last Post: 04-10-2003, 02:39 PM
  5. What happening with my Signiture Text?
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-23-2002, 07:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21