Questions about random responses.

So I have this:

 
string randomquestion [2] {"question 1", "question 2"};


can I do this:

1
2
3
4
5
6
7
8
9
randomquestion[rand() %2]
if (randomquestion == "question 1")
{
    cout <<" This is question 1";
}
else if (randomquestion == "question 2")
{
    cout <<" This is question 2";
}


So I'm identifying the random question. Can I do something like that?
Arrays don't work like that, however if you want to do something different depending on the question a switch on the index might be useful:
1
2
3
4
5
6
7
8
9
10
11
int index = rand()%2;
switch(index)
{
    case 0:
    // something here
    break;

    case 1:
    // something else here
    break;
}
If you are doing it that way, then there isn't really a point in using an array. Just use numbers:

1
2
3
4
5
6
int result = rand()%2;
if(result == 0) {
   //...
} else if(result == 1) {
   //...
}


However, this requires hardcoding a lot of the answers. I'd suggest just using the result as an index into the array:

1
2
int result = rand()%2;
std::cout<<randomquestion[result];
Topic archived. No new replies allowed.