C++ hangman | Computer Science homework help

Need help with the code


Stepwise refinement is a low level design technique in which the programmer writes pseudo-code for what the program is supposed to do in steps, expanding non-obvious steps at each iteration of the process. Eventually, the stepwise refinement will be an almost step by step guideline to the implementation of the program. 


The main objective of this assignment is to assess students’ ability to apply the stepwise refinement process to develop a new algorithm and carry that through to the implementation of the program. Implementation must follow the top-down design approach, where the solution starts by describing the general functionality of a program. Next, more details are provided in successive steps to refine the implementation.

Problem Description:

You are to implement a variant of the Hangman game, where a player must find a hidden, knowing only the first letter and number of letters in the word. The player selects a letter. If the letter is part of the word, the hidden word is updated with all occurrences of the letter. All used letters must be displayed to the player. 

In this variant, the player has 5 lives. Every time a letter is guess correctly the player gains a life. If a letter is guessed wrong, a life is removed. The game terminates if all lives are lost. If a player guesses a word correctly, another word is displayed, and game continues until the player chooses all words correctly or loses all lives. There is a total of 15 words which are read from a file.


Your task is to apply the technique of stepwise refinement to design an algorithm for the Hangman game. 

You are provided with a function to read the words and store in a string array.


A string’s individual characters can be accessed as if it was an array of characters.

string x = “hello”;

char y = x[1] //stores character ‘e’ in y

How to Write Step-Wise Refinement

Use a text editor (e.g., the Code::Blocks editor or Notepad) to write the first pseudo-code statement of the problem. Next, add more statements that support the implementation of that first pseudo-code statement. Each time you refine a step, copy and paste the current version of the program design to the end of the text file, then make the change within that pasted version. Stick to the instructor’s convention of using a different number of *s to indicate the level of expansion applicable to each statement (see example below). When the design is complete, the text file will contain a complete record of the design process used to reach the final design. The text file you submit may be quite lengthy and should show the entire process. Do not remove anything and do not just submit the final iteration. Below is a partial example of stepwise refinement (Note it is only a partial example and has nothing to do with the program for this assignment.):


#include <iostream>

#include <fstream>

#include <ctime>

#include <cstdlib>

#include <iomanip>

using namespace std;

void ReadWords(string words[], int numWords);

int main()


    int numWords = 15;

    string words[numWords];

    return 0;


void Play(string words[], int numWords){


void ReadWords(string words[], int numWords){

    ifstream infile;



        cout<<“Could not open file”<<endl;


        for(int i=0; i<numWords; i++)





void PressKeyToContinue(){

    string x;

    cout<<“Press any key to continue:”;