Question: Modify the program in C++ so that it keeps track of the letters guessed by the user. If the user enters a letter that he

Modify the program in C++ so that it keeps track of the letters guessed by the user. If the user enters a letter that he or she has already entered, display an appropriate message and do not include the letter in the number of incorrect guesses.

Here is my code so far:

#include #include using namespace std;

int main() { //declare variables string origWord = ""; string letter = ""; string guesses = ""; char dashReplaced = 'N'; char gameOver = 'N'; int numIncorrect = 0; string displayWord = "-----"; // get original word // begin loop do { cout << "Enter a 5-letter word in uppercase: "; getline(cin, origWord); while (origWord.length() != 5); } // clear the screen system("cls"); // start guessing cout << "Guess this word: "<< displayWord <> letter; if (guesses.find(letter, 0) != -1) cout << "Already entered that letter. " << endl << "Enter another." << endl; else // { guesses.insert(0,letter); // search for the letter //in the original word for (int x = 0; x < 5; x += 1) { if(origWord.substr(x,1)==letter) { displayWord.replace(x,1,letter); dashReplaced = 'Y'; } //end if } //end for // if dashed replaced //check whether the display word variable contains any dashes if (dashReplaced == 'Y') { //if display word variable does not contain any dashes, the game is over if (displayWord.find("-",0) == -1) { gameOver = 'Y'; cout << endl << "Yes, the word is" << origWord << endl; cout << "Great guessing!" << endl; } else // otherwise, continue guessing { cout << endl << "Guess this word: " << displayWord << endl; dashReplaced = 'N'; } //end if } else //processed when dashReplaced contains 'N' { // add 1 to the number of incorrect guesses numIncorrect += 1; cout << numIncorrect << "Incorrect guesses." << endl; // if the number of incorrect guesses is 10, game is over if (numIncorrect == 10) { gameOver = 'Y'; cout << endl << "sorry, the word is " << origWord << endl; } // end if } // end if } // end if } // end if } // end while return 0; } // end main function

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!