Picking a random riddle

May 18, 2010 at 4:03pm
I make loads of riddle but I need to make it pick a random riddle from it.

how would I do do it?

Thank you in advance.
Last edited on May 18, 2010 at 4:04pm
May 18, 2010 at 4:04pm
closed account (Lv0f92yv)
What do you mean by random riddle?

What database is this, and how do you plan to access it?

Edit: 60 second response - not trolling, promise.
Last edited on May 18, 2010 at 4:05pm
May 18, 2010 at 4:06pm
When I said database basicly I put made loads of riddles in the script.

Sorry i'm finding it hard to get it out my head what I mean :(
May 18, 2010 at 4:41pm
closed account (Lv0f92yv)
Do you mean some kind of file with different 'riddles' on each line? And you want to choose a random line from that file (and thus a random riddle)?
May 18, 2010 at 7:09pm
Yeah
May 18, 2010 at 7:28pm
closed account (Lv0f92yv)
Have a look at file IO: http://www.cplusplus.com/doc/tutorial/files/

and the srand function, for pseudorandom numbers.

Get the number of lines in the file, get a random number that is within that number of lines, and extract the given line based on the number using the filestream pointer.
May 18, 2010 at 7:59pm
Note that you should probably use use srand(time()).
http://cplusplus.com/reference/clibrary/cstdlib/
http://cplusplus.com/reference/clibrary/ctime/time/

To get the number of lines, use getline(ifstream, garbagestring) until you find an EOF. Maybe using good() will help?
http://cplusplus.com/reference/iostream/ios/good/

-Albatross
Topic archived. No new replies allowed.