Thread: Help with comparsion of two objects

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    Help with comparsion of two objects

    What i need to do is chekc that both objects variable name "ISBN1" have the same number.

    What i have so far is this:
    Code:
    int operator==(const ISBN& left, const ISBN& right){
      int ret = 0;
      if(strcmp(left.ISBN1, right.ISBN1){
       ret = 1;                      
      } 
      return ret;   
    }
    ...but i also need to check if incase one isbn1 has a different style, like in this case:
    123123123 is the same as 12-3123-12-3 so i should return 1 in that case, but is its 123123123 and 12-3123-12-2 then its not the same since the last number should be 3

    I think i can loop until i find a character which is not a number and ignore that one and proceed tot he next character, i dont know exactly how to get this done or if its even possible.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would massage the data to remove hyphens and then compare the new and improved versions.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would strip out all non-numeric digits first, and then compare the resulting strings (oops.. beaten).

    BTW, you are returning 1 if they are not equal, since strcmp evaluates to true if they are not equal (strcmp returns 0 for equal, a positive integer for greater than, and a negative integer for less than). Your comment makes it sound like you want it the other way around. However, since operator== usually returns a bool, you should probably just follow the canonical form and return true if they are equal and false if they are not.

    Also, is there a reason you are using C style strings here? C++ strings are generally easier to use and safer. They also already have an operator==, so after you strip the extra characters you can use:
    Code:
    return left.ISBN1 == right.ISBN1;

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    what do you mean by "massage the data"

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Rather than storing whatever the user types in, create a function called 'normaliseISBN' which takes an ISBN in a variety of formats and returns the equivalent in a uniform and consistent style which the rest of the program can deal with easily (say comparing two of them).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    On second thought, I would strongly consider storing the ISBN in the same format in the class itself, and remove the dashes when you have them input, not when you are comparing them.

    (oops, beaten again... but adding to Salem's point you should consider doing the normalization on input and storing the normalized version.)

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    ok, but im having trouble figuring out how to code a function to normalise a ISBN

    what i coded is:
    Code:
    #include <cstdio>
    #include <iostream>
    using namespace std;
    
    int main() {
      char s[11];
      char d[11];
      int n = 0;
      int e = 0;
      strcpy(s, "123-123-12");
      while(n != 0){
       if(s[n-1] >= '0' && s[n-1] <='9'){
        d[e] = s[n-1];
        e++;
       }
       if(s[n] == '\0'){
        n = 0;
       }
       else{
        n++;
       }
      }
      printf("%s", d);
    }
    also, im letting you guys know im fairly new to c++ so bare with me

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Of course, nothing happens in your loop since you set n to 0 and then immediately check while n!=0.

    You appear to have the basic idea down, although your n is ... strange. And you will want to copy the \0 to the new string as well. (ETA: And I was going to mention isdigit() as well.)

    If you were using the string class, then a lot of this would be already there waiting for you (push_back and iterators, for example; I have a feeling someone will write a one-liner using transform).

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    oh true, seems to work alright now
    Code:
    #include <cstdio>
    #include <iostream>
    using namespace std;
    
    int main() {
      char s[11];
      char d[11];
      int n = 1;
      int e = 0;
      strcpy(s, "123-123-12");
      while(n != 0){
       if(s[n-1] >= '0' && s[n-1] <='9'){
        d[e] = s[n-1];
        e++;
       }
       if(s[n] == '\0'){
        d[e] = '\0';
        n = 0;
       }
       else{
        n++;
       }
      }
      printf("%s", d);
    }

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> im letting you guys know im fairly new to c++ so bare with me
    If you're new to C++, you should really be using C++ strings.

    >> I have a feeling someone will write a one-liner using transform
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <functional>
    #include <cctype>
    
    int main()
    {
        std::string isbn("123-123-12");
        isbn.erase(std::remove_if(isbn.begin(), isbn.end(),
                   std::not1(std::ptr_fun<int,int>(std::isdigit))), isbn.end());
        std::cout << isbn;
    }

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    Quote Originally Posted by Daved View Post
    >> im letting you guys know im fairly new to c++ so bare with me
    If you're new to C++, you should really be using C++ strings.

    >> I have a feeling someone will write a one-liner using transform
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <functional>
    #include <cctype>
    
    int main()
    {
        std::string isbn("123-123-12");
        isbn.erase(std::remove_if(isbn.begin(), isbn.end(),
                   std::not1(std::ptr_fun<int,int>(std::isdigit))), isbn.end());
        std::cout << isbn;
    }
    never learned, that method before would make it alot easier though

    and, by c++ strings do you mean these because i don't think i was thought that either

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I would really recommend learning about algorithms header.

    Below is two ways to normalize the ISBN.

    The first one is simpler - in case you know that the only characters you want to remove are hyphens.

    The second one is somewhat more complicated but it removes all non-digits (although don't ISBN's allow X as the last digit?). Even if things such as std::not1 and std:tr_fun are a bit hard for you now, defining your own separate one-line function for remove_if would still be greatly simpler than writing the whole loop for stripping characters.

    Code:
    #include <cstdio>
    #include <cstring>
    #include <algorithm> // remove, remove_if
    #include <functional> // not1, ptr_fun
    #include <cctype> // is-digit
    
    using namespace std;
    
    int main() {
      char s[] = "123-123-12";
      char t[] = "ISBN: 123 34 234 5";
    
      //remove algorithms return an iterator (here pointer) to the first item (character)
      //that doesn't belong to the stripped range.
      //As these are C-style strings, a NULL-terminator goes there.
    
      //removing only hyphens
      *remove(s, s + strlen(s), '-') = '\0';
    
      //removing all non-digits
      *remove_if(t, t + strlen(t), not1(ptr_fun(isdigit))) = '\0';
      printf("&#37;s\n", s);
      printf("%s\n", t);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> by c++ strings do you mean these because i don't think i was thought that either

    Yes, that's what I mean. It's unfortunate that many books and instructors still teach C style strings and leave C++ strings until later (or not at all). If you have time and are seriously studying C++ I'd try to follow some tutorials (or skip ahead to the chapter on them in your book) and also start using them in your programs if you're allowed.

    Don't worry too much about the "one-liner" version. It's mostly for fun and you'll get more out of doing the task yourself (which you did) rather than getting complicated one-liners on forums. If you keep studying C++ then you will learn how and when to use those tools to make your life easier.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    yeah, its much simplier ill probably use it (remove_if) seeing that i can also have spaces in a formatted ISBN

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    Quote Originally Posted by Daved View Post
    >> by c++ strings do you mean these because i don't think i was thought that either

    Yes, that's what I mean. It's unfortunate that many books and instructors still teach C style strings and leave C++ strings until later (or not at all). If you have time and are seriously studying C++ I'd try to follow some tutorials (or skip ahead to the chapter on them in your book) and also start using them in your programs if you're allowed.

    Don't worry too much about the "one-liner" version. It's mostly for fun and you'll get more out of doing the task yourself (which you did) rather than getting complicated one-liners on forums. If you keep studying C++ then you will learn how and when to use those tools to make your life easier.
    yeah true, i had an idea but wasnt to sure how to start it off, thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  2. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  3. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  4. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM