I've been trying to create an array that holds two pieces of data and allows the first column to be searched but print out the second column. I've seen a few examples but I'm still stuck. Could someone show me what the array looks like if the data is
1234 example info
2345 example info
4567 example info
I think I can figure out how to search the array but I'm just not sure how to set it up.
Is this correct? I'm getting compile errors when I try to do it this way.
No, this is not correct. For starters, treeArray[5][2] is out of the bounds of the treeArray, so assigning it's address to currentElement is a nonstarter.
I would second mbozzi's recommendation to use std::map, but if you must use a two dimensional array, consider using typedefs or using declarations to make your code easier to read and write.
I haven't used map before, that looks interesting. Thanks for the suggestion as well cire, that really helped me out. I think I'll see if I can get it working with the array and then I'll see if I can figure out map.
I seem to be getting a build error stating invalid types for array subscript, could you possibly take a look at my code. I changed it a little bit because my compiler wasn't accepting the code with using leaf_type = std::string[2]. I know this seems to be more of a compiler issue but does the code look okay to you?
int main()
{
std::string tree[5][2] =
{
{"1234", "1234-abcde"},
{"2345", "2345-bcdef"},
{"3456", "3456-cdefg"},
{"4567", "4567-defgh"},
{"5678", "5678-efghi"},
};
int treeForm();
int treeData;
treeData=treeForm();
int data = treeData;
for (int* current = &tree[0]; current < tree+5; ++current)
{
if ((*current)[0] == data)
{
std::cout << data << " exists with value " << (*current)[1] << '\n';
break;
}
}
std::cout << treeData << " End of program.";
}
Thanks for the tip Thomas, it seems to be printing out all the strings though. Would you recommend putting an if loop in to check for the value I'm looking for? I don't really know how to combine your code with cire's to make the array searchable. Thanks again