472,144 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,144 software developers and data experts.

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::find(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
Jul 22 '05 #1
5 3073
<snip>
//The class Personlist contains the Person *ptr[10], and functions
//to add/remove objects.
bool Personlist::find(char* name, int & position) 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... {
bool result = true;
int i = 0;
position = -1;

while(i<number_of_elements)//number_of elements is number of
//objects in array Hint: You could already leave the loop when you have found the name in
the list and, thus, position is no more -1. {
//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). 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==.
i++;
}

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

return result;

}

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...

Jul 22 '05 #2

"Tommy Lang" <mu*****@yahoo.se> wrote in message
news:78**************************@posting.google.c om...
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::find(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

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
Jul 22 '05 #3

"Tommy Lang" <mu*****@yahoo.se> wrote in message
news:78**************************@posting.google.c om...
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::find(char* name, int & position)
That should be

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

const correctness is important in C++.
{
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).


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
Jul 22 '05 #4
John Harrison wrote:


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)


Typo:
if (strcmp(ptr[i]->get_name(), name) == 0)
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5
>
if (strcmp(ptr[i]->get_name, name) == 0)


Sorry, should be

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

john
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Federico G. Babelis | last post: by
5 posts views Thread by ritchie | last post: by
8 posts views Thread by Gerald | last post: by
204 posts views Thread by Alexei A. Frounze | last post: by
8 posts views Thread by intrepid_dw | last post: by
104 posts views Thread by Leszek | last post: by
4 posts views Thread by assgar | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.