struct searching

T

threetoast

Is there any way to have a class function to search a struct, but the
field searched be passed to it as, say, a character array? Ex:

int book::searchChar(char r[], char tyco[])
{
for (int p=0; p<x; p++)
{
if (strcmpi(library[p].tyco, r)==0)
{
return p;
}
}
return -1;
}

This doesn't work, but it's basically the idea that I want.
 
V

Victor Bazarov

threetoast said:
Is there any way to have a class function to search a struct, but the
field searched be passed to it as, say, a character array?

Sure. You just need to switch between fields based on the character
array you passed in.
Ex:

int book::searchChar(char r[], char tyco[])
{
for (int p=0; p<x; p++)
{

Add here:

if (strcmp(tyco, "field_one") == 0) {
if (strcmpi(library[p].field_one, r) == 0)
return p;
}
else if (strcmp(tyco, "field_TWO") == 0) {
if (strcmpi(library[p].field_TWO, r) == 0)
return p;
}
else if (

.... and so on
if (strcmpi(library[p].tyco, r)==0)
{
return p;
}
}
return -1;
}

This doesn't work, but it's basically the idea that I want.

It seems that you'd be better off using a database instead of programming
all that yourself.

V
 
T

threetoast

Victor said:
It seems that you'd be better off using a database instead of programming
all that yourself.

V

Databases are a little beyond me at the moment, and it's far beyond the
scope of the course I'm taking, so I'll just do it that way. Thanks!
 

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,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top