By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,117 Members | 848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,117 IT Pros & Developers. It's quick & easy.

Array problem

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
<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

P: n/a

"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

P: n/a

"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

P: n/a
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

P: n/a
>
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.