Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with struct

Status
Not open for further replies.

neillovell

Programmer
Aug 27, 2002
560
0
0
GB
Hi
How can I put the values
1 , "Letter"
2 , "Letter(small)"
3 , "Tabloid"

etc. in to a struct? I've been asked to create a lookup table that uses bsearch to find the entry with the matching number, and get the string associated.

So...
int Num = 3

// call bsearch
bsearch(Parameters);

returns "Tabloid"

I can't put the data in to the struct. How do I do this? There will be 41 entries.
 
Hi,

Could you clarify the relationship between 'Num' and 'Parameters' in your post? Are these hard-coded values or will they be loaded at run-time? I'd make it a class that has a Find/Search method - better to encapsulate that data and functionality inside a class.
 
Follow up:

If you'r going the hard-coded struct route, something like the following will work

#include <iostream>
using namespace std;

struct X
{
int i;
char* str;
};

X Tbl[3] =
{ { 1, &quot;First&quot; },
{ 2, &quot;Second&quot; },
{ 3, &quot;Third&quot; } };

int main()
{
for ( int i=0; i<sizeof(Tbl)/sizeof(Tbl[0]); ++i )
{
cout << Tbl.i << &quot;,&quot; << Tbl.str << endl;
}
return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top