Help using random function!

Feb 18, 2013 at 8:48pm
Hey guys, so i want to make a database of sorts. Dont know if this is the proper definition, but what i want to accomplish is i want to have a whole bunch of
cout texts that say different things. I then want to assign them to a char
and randomly call one of the texts in the program later on.

My understanding, is i should declare all the chars im going to assign to the different texts... if i have 3 texts
char a
char b
char c

a = cout << "random sentence a"; // and so on and so on

My question is... Am I on the right track for this idea?
My next question is... how can i consolidate this?? Is there a way to make a text file or other program just for storing a TON of these sentences, and then having the random function call randomly from that file?

Edit: I know i could use int or double instead of char, and that would probably make categorizing the sentences easier if i were to have them in this theory database of mine.
Thanks!
Last edited on Feb 18, 2013 at 8:50pm
Feb 18, 2013 at 9:02pm
Hi,

Assignment like this is invalid, but anyway, you are better off storing the sentences in an array or a vector of strings.

Feb 18, 2013 at 9:03pm
MrBananas wrote:
My question is... Am I on the right track for this idea?

Sort of, yes. But you'd be better off assigning sentences to numbers. After all, that's what your PRNG will generate.

MrBananas wrote:
Is there a way to make a text file or other program just for storing a TON of these sentences, and then having the random function call randomly from that file?

Yes. You can use fstream (http://www.cplusplus.com/reference/fstream/fstream/) for reading from files. My advice would be to store the lines in the text file, one string per line and read them in to a std::vector. Then, picking a random string is a simple as picking a random number between 0 and vector.size() - 1.
Topic archived. No new replies allowed.