Array problem

T

Tommy Lang

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
 
P

Patrik Stellmann

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

Sharad Kala

Tommy Lang said:
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
 
J

John Harrison

Tommy Lang said:
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->get_name, name) == 0)
// names are the same
else
// names are different

john
 
K

Karl Heinz Buchegger

John said:
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->get_name, name) == 0)


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top