cannot convert 'std::string' to 'int'??

So I'm going back and reviewing some old concepts in c++ and as I got to arrays I wanted to make a random color name generator using an array and the rand() function. I think I'm pretty close to getting it to work but I get the following error message on line 12:

cannot convert 'std::string {aka std::basic_string<char>}' to 'int' in assignment

I'm pretty sure I know what it means but I'm not sure how to change it to get it to compile. Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main() {
string ar[] = {"red", "orange", "yellow", "green", "blue"};
int color;
srand(time(NULL));
color = rand() % 4;
int choice;
choice = ar[color];
cout<< choice<<endl;
}


Any help is appreciated.
choice is an int.
ar[color] is a string.

You cannot assign ar[color] to choice.
I did not think just changing the type would work, and didn't even try it. That was so simple, thanks.
Topic archived. No new replies allowed.