Overview: For this project youll be creating a program to play Hangman. The goal is to get practice reading files and using arrays.

Description: For this project, you are going to create the game hangman. Hangman is a simple word game where the player attempts to guess a word before they get "hung" by guessing letters that they think might be in the word. For your version of the game, youre going to use world capitals instead of words. You will randomly select a line from the file capitals.txt; you will need to have your program count how many lines are in the file and then randomly generate a number to choose which line youll use for the game. Your program should tell the user how many letters are in the place name by showing the appropriate number of blanks for each word. Anything that is not a letter, i.e. spaces and commas, should be displayed to the screen in original form (not as a blank). Allow the user to guess letters one at a time (in either upper or lowercase) until they get the full text or guess incorrectly 6 times. Have an interface which looks similar to the one below:

_
| |
|
|
|
|
---------
The place is: _ _ _ _ _, _ _ _ _ _ _ _ _ _ _ _
Make a guess: a

As the player makes guesses, update the display accordingly.

_
| |
|
|
|
|
---------
The place is: _ a _ _ _, A _ _ _ a n _ s _ a n
Make a guess:

When the player misses, draw a new part of the person hanging from the gallows. Here is an example:

_
| |
* |
-| |
|
|
---------
The place is: _ a _ _ _, A _ _ _ a n _ s t a n
Make a guess: r
r is not in the place name
_
| |
* |
-|-|
|
|
---------
The place is: _ a _ _ _, A _ _ _ a n _ s t a n
Make a guess:

In this case, the right arm would have just been drawn because of the fourth miss. When the player gets a letter correct you should update the blank where that letter occurs to show where it goes (as was the case with s and n in the example above). When the game ends, tell what the word was and whether or not the player won. Here is an example:

_
| |
* |
-|-|
/ |
|
---------
The place is: K a b u l, A f g h a n i s t a n
You lost!

When the game ends, give the user the option of playing again.

Helpful Code and Hints: When testing your program, create a new version of capitals.txt that is smaller and easier to work with!

  • printTree() This function should print out hanging tree graphic as shown above. You will need to determine what input parameters are needed to print out the various stages of the completed diagram. You may want to create a driver program that tests out all possible options (there is only 7)
  • readFile() This function will need to read in the input file. You can pass the filename to the function. Make sure to validate that the file is opened successfully. If the function cannot open the file, it can exit the program. This function should return the number of capitals successfully read from the file. It also needs to update the array of capital data. If you notice, the input file contains blanks and commas. This will mess up the standard way of reading data. Use the following
    • Option One
      string name;
      instream.clear();
      instream.ignore();
      getline(instream, name);// allow for a 256 char name
    • Option Two
      Char name[256];
      instream.clear();
      instream.ignore();
      instream.getline(name,256); // allow for a 256 char name

makeGuess() This function will validate the users guess against the selected capital. It receives the array of capitals and the current string of guess progress. It can return a Boolean value depending on the success of the guess. It should also validate that the user has not already guessed a particular letter. You will want to ignore letter case issues 'a' vs 'A'. In order to do this, you can use the toLower(name[i]) function. In addition, you may need to use the .length() method on a string to determine how long the string is.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.