Redo Listing 16.3 so that it gets it words from a file. One approach is to use
Question:
Redo Listing 16.3 so that it gets it words from a file. One approach is to use a vector object instead of an array of string.Then you can use push_back() to copy how ever many words are in your data file into the vector object and use the size() member to determine the length of the word list. Because the program should read one word at a time from the file, you should use the >> operator rather than getline().The file itself should contain words separated by spaces, tabs, or new lines.
Here’s a sample run of the program in Listing 16.3:
Will you play a word game? y
Guess my secret word. It has 6 letters, and you guess
one letter at a time. You get 6 wrong guesses.
Your word: ------
Guess a letter: e
Oh, bad guess!
Your word: ------
Bad choices: e
5 bad guesses left
Guess a letter: a
Good guess!
Your word: a--a--
Bad choices: e
5 bad guesses left
Guess a letter: t
Oh, bad guess!
Your word: a--a--
Bad choices: et
4 bad guesses left
Guess a letter: r
Good guess!
Your word: a--ar-
Bad choices: et
4 bad guesses left
Guess a letter: y
Good guess!
Your word: a--ary
Bad choices: et
4 bad guesses left
Guess a letter: i
Good guess!
Your word: a-iary
Bad choices: et
4 bad guesses left
Guess a letter: p
Good guess!
Your word: apiary
That's right!
Will you play another? n
Bye
Step by Step Answer: