Array problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tommy Lang

    Array problem

    Hi!

    I have aa array with pointers to the class(object) Person that I
    created
    ( Person *ptr[10]; ). From class person there are two derived classes,
    student and employee. I can add and remove objects of these types in
    my array. Now I want to be able to search for a specific object of my
    choice based on the persons name. How do I do that?? I need help.

    So far this is what I've come up with...

    //The class Personlist contains the Person *ptr[10], and functions
    //to add/remove objects.
    bool Personlist::fin d(char* name, int & position)
    {
    bool result = true;
    int i = 0;
    position = -1;

    while(i<number_ of_elements)//number_of elements is number of
    //objects in array
    {
    //The Person class have a variable called m_name.
    //How can I reach it from here and compare it to what
    //name is sent into this function? For now I just want
    to
    //return true or false (and reference to position).

    i++;
    }

    if(position == -1)
    result = false;

    return result;

    }


    Thanks,
    Tommy
  • Patrik Stellmann

    #2
    Re: Array problem

    <snip>[color=blue]
    > //The class Personlist contains the Person *ptr[10], and functions
    > //to add/remove objects.
    > bool Personlist::fin d(char* name, int & position)[/color]
    You should use 'const char* name' since you're not goint to modify that
    string! Even better would be to use std::string instead of char-arrays...[color=blue]
    > {
    > bool result = true;
    > int i = 0;
    > position = -1;
    >
    > while(i<number_ of_elements)//number_of elements is number of
    > //objects in array[/color]
    Hint: You could already leave the loop when you have found the name in
    the list and, thus, position is no more -1.[color=blue]
    > {
    > //The Person class have a variable called m_name.
    > //How can I reach it from here and compare it to what
    > //name is sent into this function? For now I just want
    > to
    > //return true or false (and reference to position).[/color]
    The function to compare to char-arrays is strcmp which returns 0 if both
    are equal. If you'd use std::string you can simply use the operator==.[color=blue]
    >
    > i++;
    > }
    >
    > if(position == -1)
    > result = false;
    >
    > return result;
    >
    > }[/color]
    You should also consider using std::map instead of an array to store the
    Person-pointers since it is much more flexible and more comfortable to
    use...

    Comment

    • Sharad Kala

      #3
      Re: Array problem


      "Tommy Lang" <mums_se@yahoo. se> wrote in message
      news:78dde37d.0 402250037.5a1a6 486@posting.goo gle.com...[color=blue]
      > Hi!
      >
      > I have aa array with pointers to the class(object) Person that I
      > created
      > ( Person *ptr[10]; ). From class person there are two derived classes,
      > student and employee. I can add and remove objects of these types in
      > my array. Now I want to be able to search for a specific object of my
      > choice based on the persons name. How do I do that?? I need help.
      >
      > So far this is what I've come up with...
      >
      > //The class Personlist contains the Person *ptr[10], and functions
      > //to add/remove objects.
      > bool Personlist::fin d(char* name, int & position)
      > {
      > bool result = true;
      > int i = 0;
      > position = -1;
      >
      > while(i<number_ of_elements)//number_of elements is number of
      > //objects in array
      > {
      > //The Person class have a variable called m_name.
      > //How can I reach it from here and compare it to what
      > //name is sent into this function? For now I just want
      > to[/color]
      Ibelieve Personlist ia nother class different from person.
      Add a public member function to your Person class.
      It should return you the m_name varaible.
      Then use strcmp to compare the values in this function.
      Even better and safe is to use std::string.

      -Sharad


      Comment

      • John Harrison

        #4
        Re: Array problem


        "Tommy Lang" <mums_se@yahoo. se> wrote in message
        news:78dde37d.0 402250037.5a1a6 486@posting.goo gle.com...[color=blue]
        > Hi!
        >
        > I have aa array with pointers to the class(object) Person that I
        > created
        > ( Person *ptr[10]; ). From class person there are two derived classes,
        > student and employee. I can add and remove objects of these types in
        > my array. Now I want to be able to search for a specific object of my
        > choice based on the persons name. How do I do that?? I need help.
        >
        > So far this is what I've come up with...
        >
        > //The class Personlist contains the Person *ptr[10], and functions
        > //to add/remove objects.
        > bool Personlist::fin d(char* name, int & position)[/color]

        That should be

        bool Personlist::fin d(const char* name, int & position) const

        const correctness is important in C++.
        [color=blue]
        > {
        > bool result = true;
        > int i = 0;
        > position = -1;
        >
        > while(i<number_ of_elements)//number_of elements is number of
        > //objects in array
        > {
        > //The Person class have a variable called m_name.
        > //How can I reach it from here and compare it to what
        > //name is sent into this function? For now I just want
        > to
        > //return true or false (and reference to position).[/color]

        Are you using a character array or a string to store the name? You should be
        using a string but many newbies don't because they aren't taught right. I'm
        going to assume a character array.

        You should have something like this in your Person class

        class Person
        {
        public:
        ...
        const char* get_name() const { return m_name; }
        ...
        private:
        ...
        char m_name[99]; // name is a character array
        ...
        };

        Then in your loop you use strcmp to compare the passed in name with the
        person name.

        if (strcmp(ptr[i]->get_name, name) == 0)
        // names are the same
        else
        // names are different

        john


        Comment

        • Karl Heinz Buchegger

          #5
          Re: Array problem

          John Harrison wrote:[color=blue]
          >
          >
          > class Person
          > {
          > public:
          > ...
          > const char* get_name() const { return m_name; }
          > ...
          > private:
          > ...
          > char m_name[99]; // name is a character array
          > ...
          > };
          >
          > Then in your loop you use strcmp to compare the passed in name with the
          > person name.
          >
          > if (strcmp(ptr[i]->get_name, name) == 0)[/color]

          Typo:
          if (strcmp(ptr[i]->get_name(), name) == 0)


          --
          Karl Heinz Buchegger
          kbuchegg@gascad .at

          Comment

          • John Harrison

            #6
            Re: Array problem

            >[color=blue]
            > if (strcmp(ptr[i]->get_name, name) == 0)
            >[/color]

            Sorry, should be

            if (strcmp(ptr[i]->get_name(), name) == 0)

            john


            Comment

            Working...