How to find the number of the element inside an array?

I have 4 arrays, and all has same number of elements inside it. I want to get the number of the element in array1 to retrive the other elements in the other arrays

for example

array names = david, James, Jessica, Laura
array cars = Ford, Nissan, Toyota, BMW
array color = green, blue, red, pink
array food = burger, icecream, eggs, sushi

now I want the user to enter only one piece of information from the arrays above, and based on that I will get the rest of the information. For example: I will ask the user first what type of information are you planning to use? let's say the user said "color", now I ask the user to enter the color -> user entered "pink"
now I go to array "color" and find where is "pink? and based on that I will give him the rest of the information like this:
Color pink
name Laura
car BMW
food sushi


Any ideas how to do this? I know how to deal with arrays I just don't know how to find the number of an element based on a user entry!
Oop was built for this kind of problem. In this case you would have a class\struct called CAR or something with four string values. Like this (I'll change some of the variable names so that it is a little easier to read):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct CAR
{
   private:
   
   public:
      CAR(std::string owner, std::string brand, std::string color, std::string food): Owner(owner), Brand(brand), Color(color), Food(food) {}

std::string Owner;
std::string Brand;
std::string Color;
std::string Food; //????

void Display()
{
   std::cout << "Color: " << Color << //etc...
   //etc...
   //etc...
}

};

But I need to know what exactly the number of the element inside the array so I can grab the other element's value from the other arrays! Because I have 118 element in each array and some of my arrays are strings and some are doubles or ints!

Thank you Computergeek01 !


Write a general function that returns the position of a target string in an array.

For example

1
2
3
4
5
6
7
8
size_t find( std::string a[],. size_t n, std::string s )
{
   size_t i = 0;

   while ( i < n && a[i] != s ) i++;

   return ( i );
}


If the string will be found the function returns its position in the array. Itherwise it will return the size of the array that is the index just after the last acceptable for the array. You can pass any of your arrays to the function

Of course it is better to use standard algorithm std::find. But I am afraid that you do not know it yet.
Last edited on
You have to keep track of the size of the array yourself.

If you're reading from a file (I hope), keep track of the amount of loops you do. Or perhaps put the size of the array in the file.
Topic archived. No new replies allowed.