Comparing A Struct To A Passed Parameter

This is a discussion on Comparing A Struct To A Passed Parameter within the C++ Programming forums, part of the General Programming Boards category; I have a program that I need to do for class and actually want to learn something so I don't ...

  1. #1
    Anonymous
    Guest

    Question Comparing A Struct To A Passed Parameter

    I have a program that I need to do for class and actually want
    to learn something so I don't want to post my entire program.
    Basically what I have is a dynamically allocated arrays that have structs. My problem is I'm trying to do a comparision on the passed in InputIsbn to see if it matches with something in the
    ptrList[index]->Isbn if it does I want to return the index if not
    a -1.

    Does anyone have any ideas to where the problem may lie?
    I know I must be making some small error that is messing everything up. This seems like it should work but it doesn't. Anyone have any ideas?

    int BookList::SearchIsbn(string InputIsbn)
    {

        int index;
        int comparison;

    &nbsp;&nbsp;&nbsp;&nbsp;for (index = 0; index < Length; index++)
    &nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;co mparison = InputIsbn.compare( ptrList[index]->Isbn );
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;if (comparison == 0)
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;cout << "The two strings are the same\n";
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;return index;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;}

    &nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;return -1;

    }

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Question I would like to help.. but..

    I would like to help yah out there mate.. but to be honest I am having a tough time trying to follow ur code..hard with the little bit that you have supplied..plus I do not know what kind of error you are receiving..

    EDIT:
    hmm... not sure how you are calling the function
    SearchIsbn(string InputIsbn)

    is this similar to ur code?

    Code:
    struct BookList
      {
        string Isbn;
        int SearchIsbn(string);
      }
    
    int main()
      {
         BookList* ptrList[20];
      }
    in order to call SearchIsbn wouldn't u have to type
    Code:
    ptrList[0]->SearchIsbn("Bob");
    ?? I believe you would have to pass in one index at a time wouldn't u?
    Code:
    ptrList->SearchIsbn("Bob")  // I don't think this is possible..
    I hardy use pointers.. let alone structs so i may be way off... if this were the case then inside ur function definition..

    Code:
    comparison = InputIsbn.compare( ptrList[index]->Isbn );
    would need to be changed to
    Code:
    comparison = InputIsbn.compare( Isbn );
    like i said earlier.. hard to pinpoint ur problem when i have no idea what it is.. how u are calling the function.. what the struct looks like.. etc... this is the best i could come up with..

    perhaps this code could be some assistance
    Code:
    struct BookList
      {
        string Isbn;
        int SearchIsbn(string);
      }
    
    int main()
      {
         BookList* ptrList[20];
         ptrList[0]->Isbn = "Hello";
         int answer;
         for(index = 0; index < 20; index++)
           {
             answer = ptrList[index]->SearchIsbn("Hello");
             if (answer != -1)
                break;
           }
         cout << answer;
        return 0;
       }
    
    int BookList::SearchIsbn(string InputIsbn)
    {
    
       
       if (InputIsbn.compare( Isbn ) == 0)
          {
              cout << "The two strings are the same\n";
             return index;
          }
       return -1;
    }
    this code has not be tested.. just so u know.. i'm too lazy..

    ICQ#173580303
    Last edited by tegwin; 11-10-2002 at 12:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Base Class Constructor parameter has a struct
    By cloudy in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2007, 02:24 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 12:25 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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