ok so last time I was here, I was programmin tic-tac-toe and its great. Now I have programmed hangman! :D . Anyways right now its for two players only. So I want it so the computer will choose a random word from a text file and assign that to my string called word. I know it requires ifstream but cant get it to work. If someone could write out the necessary code, that would be great! My txt file is called "hangtext.txt."

Looking at my actual hangman code is not necessary. All you have to know is that a random word needs to be selected from a text file (one word per line in the txt file, in case that affects anything), and set equal to my string word. I am much appreciative. thanks!

>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 number first, 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.

um my word is actually

char word[15];

because i dont have string headers and vector headers.

>because i dont have string headers and vector headers
So use that instead of a string and an array instead of a vector. Is it really so hard to translate my example into something that works for you?

I suppose you'll run into problems if the word ends up being "superconductors", or "Lackawannanians", or "indistinguishable", or "incomprehensible".

>because i dont have string headers and vector headers
So use that instead of a string and an array instead of a vector. Is it really so hard to translate my example into something that works for you?

well i try that but it always comes up with an error sych as "23 invalid conversion from `char*' to `int' ". It's not hard to translate, It just doesn't work for me. Then i changed return word; to cout<<word; because I assumed they did the same thing. And it works, but doesnt print a random word, it prints one specific word off the list. Heres what i got.

#include <iostream.h>
#include <stdlib.h>
//#include <conio.h>
#include <stdio.h>
#include <fstream.h>
#include <time.h>
#include <iomanip.h>


   main(){
ifstream in ( "hangword.txt" );
char word[15];

if ( !in )
  return 0;

int r = rand() % 5+0; // Assuming 10,000 words in the file

do
  in>> word;
while ( --r >= 0 );

cout<< word;
    system("pause");
    return 0;
}

You haven't used srand to seed the random number generator with say, the current time, so the random number generator outputs the same sequence of numbers every time you run the program.

You haven't used srand to seed the random number generator with say, the current time, so the random number generator outputs the same sequence of numbers every time you run the program.

thank you again rashakil Fol, you have helped me once again. And thank you Narue. And like I said I have hangman already written up and wasn't just asking for the code, I was confused about a piece of it and when I asked fori t to written out, I meant just that piece so I can understand. Thanks.

This article has been dead for over six months. Start a new discussion instead.