c++ help - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: c++ help (/showthread.php?tid=26275)
c++ help by Muss on 05-29-2004 at 07:32 AM
I am needing to RANDOMLY inport a string from a txt file.
I know how to get strings, single lines, single characters etc in order, but I have no idea how to jump to a random place, and get that entire word.
The way the txt file is set up is:
Word1
Word2
Word3
Word4
Word5
etc
So basically, the word, \n, another word, \n etc
Anyone able to help me with that?
[edit]
Also, I would prefer it if the person could explain why you do what, instead of just a straight code paste. I would find it alot more useful to understand why I am doing what, instead of blindly following
Cheers
Muss
RE: c++ help by Mike on 05-29-2004 at 08:36 AM
If i understand correcly you want to get the text of a line from a txt file eh?
Well what i would do in vb:
Put the text file in a string...
Find how many lines the text file has so i can have a random number from 1 to the lines of the file.
Then use split() on the string that has the text and in the ()s put the random generated number
And get the final result.
Hopes this helps you!
Good luck
Btw i posted what i would do in vb, not in C++...
RE: c++ help by Muss on 05-29-2004 at 08:52 AM
Well that doesn't help then
Anyway I fgured out A way to do it, probably isn't very efficient, but it works well enough.
Generate random number
Enter For loop that loops until 'i' = the random number, and increment 'i'
In the loop read in a line of the text.
'i' then gets incremented, and is checked if it is = to the random number
if it isn't, we carry on the loop and the next line of text is read.
That happens until 'i' = the random number.
And thus have a semi random word
(used the srand time random thingy, so it generates a random number in relation to the time since some date, so it is basically truely random )
Thanks for attempting to help mike
|