Exception thrown

I was trying to make an array picking random from another array, but it throws an exception, I wonder why, here is my code:

class mastermind {
private:
string color[9] = { "red", "orange", "yellow", "green", "cyan", "blue", "purple", "white", "black" };
string pick[4] = { " "," "," "," " };
public:
mastermind() {};
void setColor() {
for (int i = 0; i < sizeof(pick); i++) {
int random = rand() % 9;
string temp = color[random];
pick[i] = temp;
}
};

};
> for (int i = 0; i < sizeof(pick); i++)
This doesn't tell you how many elements an array has.

This does.
sizeof(pick) / sizeof(pick[0])
1
2
3
4
5
6
7
void setColor() {

    const int NCOLORS = sizeof(color) / sizeof( color[0] ) ; // number of colors in color array
    const int NPICKS = sizeof(pick) / sizeof( pick[0] ) ; // number of colors in pick array

    for( int i = 0; i < NPICKS ; ++i ) pick[i] = color[ std::rand()% NCOLORS ] ;
}
Topic archived. No new replies allowed.