Extracting words from a datafile

Hello,
I am fairly new to programming and in class we are making a hangman game as a semester long project. Previously we had to use a word from a data file to run the game on. our next assignment is to be able to extract a word LIST from the data file, without knowing the amount of words in the file. The program is supposed to be able to list each word in the file and then set the last word of the file to our word variable.. I'm trying to use a while loop to accomplish this but I really am confused in how to correctly write it.

here is my part i need help on
....

infile.open("wordlist.dat");

if(!infile)
{
cout << "Error opening file\n";
system("pause");
return 1;
}
//extracting the words into string literals
infile >> word;
while (!NULL)
cout << "Word: " << word << endl;

...
when i run this my program gets hung on the first word and repeats it forever.

i have also tried...

//extracting the words into string literals
infile >> word;
while (!infile.eof())
cout << "Word: " << word << endl;

...
same thing happens for both circumstances... Is it because the "infile" is getting set before the conditional? when I put it in the while statement the programs doesn't output ANYTHING and just has a curser in the top left of the cmd terminal. help is appreciated! thx
your code reading only one word from the file. the while(!NULL) or while(!infile.eof()) loops are not reading the word from the file, it just check the condition.


1
2
3
4
while(!infile.eof())
{
infile>>word;
cout<<word<<endl;
awesome man! thanks for your help
Topic archived. No new replies allowed.