Storring a user entered variable from an Array

Hello so I am trying to store the index number into a separate variable, but cant for the life of me figure out how. Any help would be appreciated. This is what i have so far.

string colors[] = {
"Black", "Brown", "Red", "Orange", "Yellow",
"Green", "Blue", "Violet", "Gray", "White"
};
cout << "Please enter a color: ";
cin >> user1;

for (int i = 0; i<10; i++) {
if (colors[i] == user1) {
cout << "Color _" << colors[i] << "_ has value " << i << endl;
break;
}
}
closed account (48T7M4Gy)
cout << "Color _" << user1 << "_ has value " << i << endl;
That value of i is what i am trying to get stored in a different variable. Big picture I need to repeat this process three times. And calculate the resistance using a formula R=(10a+b)*10c. But Im having trouble saving that 'i' value into a variable to be used in another place
Last edited on
closed account (48T7M4Gy)
So,

1
2
3
4
5
6
7
8
9
10
11
12
int differentVariable = 0;

...
for( )
   {
      ...
      if (...)
      {
         ... 
         differentVariable = i;
      }
}
Last edited on
closed account (48T7M4Gy)
we crossed over but see previous
closed account (48T7M4Gy)
If you are doing it repeatedly then make differentVariable[] an array and index the problem with an outer (new) loop.
Thank You!!!
Last edited on
Topic archived. No new replies allowed.