473,499 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3284
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3231
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
9
4765
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
5
2186
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
8
4589
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line...
12
5483
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
204
12883
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
8
10679
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
104
16845
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
23
7364
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
4
1966
by: assgar | last post by:
Hi Seasons Greetings Its back, I am being haunted. I thought I had resolved this problem but I am intermittently the receving the warnings below. This code consist of a 1) HTML form, 2)...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7012
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.