Activity: Hangman! getString (iPad)

Hangman is a simple word game for one player. The system thinks of a word and the player tries to guess that word by guessing one letter at a time. If the guessed letter is contained in the word, then the player is shown its location(s) in the word. If the letter is NOT contained in the word, then that wrong guess causes a body part to be drawn on the “gallows”. If the player makes six incorrect guesses before guessing the correct word, the entire body has been drawn and the game is lost.

Working with two partners, you will begin writing a C++ program which simulates a primitive game of Hangman. You will begin by writing a function that will be used in the program, and then testing the function using a simple unit test.

The Assignment

Working with two partners, you will write a C++ program which simulates a primitive game of Hangman. You will begin by writing a function. This function will prompt the user for his/her “guess” (a character, though we will store it as a string). The function prototype should be:

string getString(string prompt);

This function will do the following:

  • Prompt the user, using the parameter value as the message;
  • Input the value as a string, using the getline() function;
  • Return the user input to the calling function.

Don’t forget to follow the C++ Coding Guidelines!

To test your function, add the function prototype and the function itself to the following code:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main() 
{
   // declare storage for the input... 
   string user_input; 
 
   user_input = getString("Please enter a word: "); // get the user input
   cout << "You entered: " << user_input << endl;   // echo-print the input 

   user_input = getString("Please enter a sentence: ");// get the user input
   cout << "You entered: " << user_input << endl;    // echo-print the input
 
   user_input = getString("Please enter a letter: "); // get the user input
   cout << "You entered: " << user_input << endl;     // echo-print the input
   
   return 0;
}

Make sure you test the program using appropriate inputs, as directed by the input prompts. When you have completed the program, add a Box Note to the Box folder for Hangman containing only your function and the function prototype – not the entire program. Name the Box Note with your names and the function name.