473,326 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

struct searching

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.

Jul 23 '05 #1
2 1382
threetoast wrote:
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
Jul 23 '05 #2

Victor Bazarov wrote:
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!

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: James Harris | last post by:
Having updated my Debian system it now complains that I am using an incompatible pointer type in warnings such as "passing arg 2 of 'bind' from incompatible pointer type" from code, struct...
258
by: Terry Andersen | last post by:
If I have: struct one_{ unsigned int one_1; unsigned short one_2; unsigned short one_3; }; struct two_{ unsigned int two_1;
20
by: Elliot Marks | last post by:
If a struct or its members are passed to a function, must it be declared globally? #include <stdio.h> struct mystruct{ int a; int b; }; int structfunc(struct mystruct foo);
8
by: J Krugman | last post by:
My compiler complains if I do something like this typedef struct { node *next; } node; but it's OK with typedef struct superfluous_identifier { superfluous_identifier *next;
10
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
20
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
4
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup }...
9
by: werasm | last post by:
Hi all, What is the difference between: typedef struct { ... } MyS1; ....and...
3
by: Hallvard B Furuseth | last post by:
to find the required alignment of a struct, I've used #include <stddef.h> struct Align_helper { char dummy; struct S align; }; enum { S_alignment = offsetof(struct Align_helper, align) };
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.