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
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.

![const int NUM = 26; const string wordlist [NUM] = {"apiary", "beetle",](https://dsd5zvtm8ll6.cloudfront.net/si.question.images/images/question_images/1662/1/1/6/2756311e1b3927221662116276339.jpg)


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
Listing 16.3 hangman.cpp // hangman.cpp #include #include #include #include #include using std::string; const int NUM = 26; const string wordlist [NUM] = {"apiary", "beetle", "cereal", "danger", "ensign", "florid", "garage", "health", "insult", "jackal", "keeper","loaner", "manage", "nonce", "onset", "plaid", "quilt", "remote", "stolid", "train", "useful", "valid", "whence", "xenon", "yearn", "zippy"}; some string methods int main() { using std::cout; using std::cin; using std::tolower; using std::endl; std::srand (std::time (0)); char play; cout < < "Will you play a word game? "; cin >> play; play= tolower (play); while (play== 'y') { string target= wordlist [std::rand () & NUM]; int length target.length(); string attempt (length, '-'); string badchars; int guesses = 6; cout < < "Guess my secret word. It has " < < length
Step by Step Solution
3.33 Rating (156 Votes )
There are 3 Steps involved in it
Here is a modified version of Listing 163 that reads words from a file include include include inclu... View full answer
Get step-by-step solutions from verified subject matter experts
