>If someone could write out the necessary code, that would be great!
I'm sure it would be, but that's not how it works. You write the code, and we help you with it. Too many people show up expecting us to write everything for them, and that's not the point of this forum.
>I want it so the computer will choose a random word from a text file
This is a surprisingly interesting problem, and you can go about it several different ways. If the file is small enough, you can read it into fast memory, generate a random number in the range of [0..N) where the file contains N words, then pick the one at that index:
ifstream in ( "somefile" );
vector<string> words;
string word
if ( !in )
return;
while ( in>> word )
words.push_back ( word );
return words[rand() % words.size()];
Of course, that's not memory efficient and if the file is a full dictionary listing then it's not practical either. If you know how many words are in the file then you can generate the random numberfirst, and read that many words, returning the most recent word when the random number reaches 0:
ifstream in ( "somefile" );
string word;
if ( !in )
return;
int r = rand() % 10000; // Assuming 10,000 words in the file
do
in>> word;
while ( --r >= 0 );
return word;
Of course, even though that is memory efficient, it assumes that you know how many words are in the file. If you don't know this, you need to make a guesstimate. One popular solution is to read the entire file, and randomly select a survivor of two or more words:
ifstream in ( "somefile" );
string word, survivor;
in>> survivor;
while ( in>> word ) {
if ( rand() < RAND_MAX / 2 )
survivor = word;
}
return survivor;
You can change the weighting of whether the survivor is modified by changing the 2 in RAND_MAX / 2 to another constant, or even a variable that changes as more of the file is read. This is a flexible technique.