I have a four digit number that I need to search an array for and print a string of words. I've seen a few examples but it was hard to relate it to my project I'm working on. Could someone help me compare my treeData integer to an array? The data will look like "1123" and "Tree information goes here". I need to search using the 1123 and print out "Tree information goes here.".
Here is the code I'm working on.
main
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "include/treedata.h"
int main()
{
tree_1111 treeInfo;
int treeForm();
int treeData=0;
treeData=treeForm();
std::cout << treeInfo.getInfo();
std::cout << treeData << " End of program.";
}
First off, you should not be using goto. This is 2017 we use structured programming, not this "spaghetti code"! Its bad practice and just leads to a mess! Next, the array just trying to understand how its setup and I don't see any array in your code. From what you say I envision it as:
|----------------------
| Tree # | Tree info |
|----------------------
| 1124 | Tree info |
|----------------------
| 1123 | Tree info |
|----------------------
| continued |
If this is what it looks like just use a for loop and test the values in the first column. Just stepping through each one is simplest but there are many more complicated but more efficient search algorithms out there.
How do I test for the first value but print the second value? I think I'm a bit lost on the concept. I talked with my professor before using goto and he was okay with it, what's so wrong about it if it's isolated in a function? Sorry, not trying to be difficult or anything!
You can look at each value in the array individually when trying to find the number you want you'll increment your row number, while looking in the first column, till you find what your looking for, then save that number because you can then use that row number but look in the second column for the tree info.