Just a shot in the dark ... Traverse the text find and find out how many lines there are. Then, use a random number generator to generate a random number between 0 and the number of lines and store this as random number X. Then, traverse the text file once again going down X number of lines. Then read that line.
>how do you get a RANDOM word from a text file?
Unless the file is so large that placing it in memory is prohibitive, or you're under stupid restrictions, you read the text file into a suitable data structure that gives you convenient and efficient searching.
Suggestion - create an array of pointers, and as you read in each word, use strdup() to put it on the heap, and save the new pointer in an array somewhere. This way when you get your pseudo-random number, you can just use that as the index of the array lookup (after folding the number down to the array bounds, like csgal said).
As Narue was saying, cleanest way is to load each word from the file into something like a vector<string> myWords, then generate a random number between 0 and the vector's myWords.size(). Then you can simply access the vector via myWords[randomNum]
Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...
Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...