Write a C++ program for the classic hangman game - guess the word without getting more than 6 letters wrong. The user is informed of the number of letters in the word to be guessed. A wrong guess of the letter would cause additional part of the hangman figure to be drawn. A correct guess would cause the letter to be displayed at the appropriate position of the word.

The program should not accept repeated entry of the same letter. The user is to be informed through a message that this letter has already been guessed.

The word to be guessed is to be read from a text file.

The game ends when the word has been fully guessed or the user gets more than 6 letters wrong.

Design and implement at least one class in your program.

A possible output for your driver program is given below. The keyboard inputs by user are highlighted in yellow.

You are required to exercise to creativity in your program design. The output below serves only as a guide and you are expected to further enhance this output and provide additional features. Also, you should make your program as friendly as possible to the users.


Anyone can help me with this? Like a guideline to get started. I'm using visual studio and please teach me in C++ basic thx. I'll show u guys my hardwork if i got a guideline thx in advance

sry i dun get it. I just know i'm using visual studio 2005. Its a basic OOP C++ program that i need.

Well then perhaps you should start by writing your ideas down on paper. What methods your class will need etc.

What wee is saying in his own way is you must do something before we can help you. All we can do with the info you've given is write it for you.

Please read this as well as The Forum Rules

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

const int MAXLENGTH=80;
const int MAX_TRIES=5;

int letterFill (char, char*, char*);
void initUnknown (char*, char*);
void getWord (char*);

void main () {
  
  //
  // Variables:
  
  char word    [MAXLENGTH];
  char unknown [MAXLENGTH];
  char name    [MAXLENGTH];
  char letter;
  int num_of_wrong_guesses=0;
  ofstream fout("output.dat");
  
  //
  // program code:
  
  // Get a random word from the data file:
  getWord (word);
  
  // Initialize the secret word with the * character.	
  initUnknown(word, unknown);

  // welcome the user
  cout << endl << endl << "Welcome to hangman...without the graphics. :)";
  cout << endl << endl << "Each letter is represented by a star." << endl;
  cout << "You have " << MAX_TRIES << " tries to try and guess the word.";
  cout << endl << "Please enter your name " << flush;
  cin >> name;
  cout << endl;

  // echo to log file
  fout << "Welcome to hangman...without the graphics. :)";
  fout << endl << "Each letter is represented by a star." << endl;
  fout << "You have " << MAX_TRIES << " tries to try and guess the word.";
  fout << endl << "Please enter your name " << name << endl;
  
  // Loop until the guesses are used up
  while (num_of_wrong_guesses < MAX_TRIES) {
      
    // Display the unknown word/phrase
    cout << unknown << endl;
    cout << "Guess a letter: " << flush;
    cin >> letter;
    fout << unknown << endl;
    fout << "Guess a letter: " << letter;
      
    // Fill secret word with letter if the guess is correct,
    // otherwise increment the number of wrong guesses.
    if (letterFill(letter, word, unknown)==0) {
      cout << endl << "Whoops!  That letter isn't in there!" << endl;
      fout << endl << "Whoops!  That letter isn't in there!" << endl;
      num_of_wrong_guesses++;
    } else {
      cout << endl << "You found a letter! Isn't that exciting!" << endl;
      fout << endl << "You found a letter! Isn't that exciting!" << endl;
    }
      
    // Tell user how many guesses he/she has left.  
    cout << "You have " << MAX_TRIES-num_of_wrong_guesses;
    cout << " guesses left." << endl;
    fout << "You have " << MAX_TRIES-num_of_wrong_guesses;
    fout << " guesses left." << endl;
      
    // Check if they guessed the word.
    if (strcmp(word, unknown) == 0) {
      cout << word << endl;
      cout << "Yeah!  You got it!" << endl;
      fout << word << endl;
      fout << "Yeah!  You got it!" << endl;
      exit(0);
    }
  }
  cout << "Sorry, you lose...you've been hanged." << endl;
  cout << "The word was : " << word << endl;
  fout << "Sorry, you lose...you've been hanged." << endl;
  fout << "The word was : " << word << endl;
}

// Take a one character guess and the secret word, and fill in the 
// unfinished guessword. Returns number of characters matched.
// Also, returns zero if the character is already guessed.
int letterFill (char guess, char *secretword, char *guessword ) {
  int i;
  int matches=0;
  
  for (i = 0; i < MAXLENGTH; i++) {
    // Are we at end of word?
    if ( secretword[i] == 0 ) break;
      
    // Did we already match this letter in a previous guess?
    if (guess == guessword[i]) return 0;

    // Is the guess in the secret word?
    if (guess == secretword[i]) {
      guessword[i] = guess;
      matches++;
    }
  }
  return matches;
}

//
// Initialize the unknown word
//
void initUnknown (char *word, char *unknown) {
  int i;
  int length = strlen(word);
  
  for (i = 0; i < length; i++) unknown[i]='*';
  unknown[i] = 0;
}

//
// gets the random word from the file
//
void getWord(char *buf) {
  static ifstream fin("p:/course/cs302/public/Program0/hangman.dat");
  int x;
  int count=1;
  int wc;
  int i = 0;
  
  // initialize the random number generator
  srand(time(0));

  // use the random number
  wc = rand()%20;

  // move to the correct place in the file
  while (count < wc) {
    fin >> x;
    if (x==0) count++;
  }
  
  // read in the word
  do {
    fin >> x;
    buf[i++] = char (x);
  } while (x);
}

This is what i can think of. Help please

Not only does the post lack code tags, I see void main and various usage of ) and ; mangled into smilies.
Oh well, better luck next time.

One thing (amongst the many other things, that I don't have the effort to mention) is this function:

// gets the random word from the file
//
void getWord ( char *buf )
{
  static ifstream fin ( "p:/course/cs302/public/Program0/hangman.dat" );
  int x;
  int count = 1;
  int wc;
  int i = 0;

// initialize the random number generator
  srand ( time ( 0 ) );

// use the random number
  wc = rand() % 20;

// move to the correct place in the file
  while ( count < wc ) {
    fin >> x;
    if ( x == 0 ) count++;
  }

// read in the word
  do {
    fin >> x;
    buf[i++] = char ( x );
  } while ( x );
}

Does this do what you think it does?

This article has been dead for over six months. Start a new discussion instead.