How to display strings randomly...

Is there anyway I can display strings randomly in C++?

eg:
1
2
3
string a,b;
a="Chicken tigh.";
b="Fish";


then when I open the program, it displays either string a or b.
closed account (o3hC5Di1)
You can put them in an array, then have a random index for the array generated.

Functions you will need:

http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

Hope that helps.

All the best,
NwN
Thanks!!!
@NwN

Can I have an example code please?!?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// seed srand
srand ( time(NULL) );

// add some strings to a vector
std::vector<std::string> svec;
svec.push_back("Cake");
svec.push_back("Fish");
svec.push_back("Cookie");

// find the size of the vector
size_t max_size = svec.size();

// generate the random number between 0 and the size of the vector
const int random = (rand() % max_size) + 0;
	
std::cout << svec[random] << std::endl;
Last edited on
Thank you all!!!
Topic archived. No new replies allowed.