I have a couple of sentences in an array. I need to randomly display 1 of them. How can I do this?

Recommended Answers

All 3 Replies

Lets say you have 5 strings, you need to generate a randome number between 0 and (but not including) 5. Use the mod operator % to accomplish that int num = rand() % 5;

right but I am not using int i am using an array. So what would the systax be

char array1[j] = rand () % i;

???

I have a couple of sentences in an array. I need to randomly display 1 of them. How can I do this?

So what would the systax be

char array1[j] = rand () % i;

???

no it is not like that. you do not set the elements of the array to be a number from 0-5, what you want is just to display the string as you said in your original post cout << array[rand() % 5] << "\n"; Note in the above the value 5 assumes you have 5 strings. Replace that with however many strings you actually have in your array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.