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.

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

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

1 Expert Approved Answer
Step: 1 Unlock

Here is a modified version of Listing 163 that reads words from a file include include include inclu... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Introduction Java Program Questions!