Hangman Project

Just started a C++ class at school and I am having a really hard time with a hangman assignment. This is what I have so far, it's not much.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int MAX_WORDS = 50;
struct wordDictionary
{
string word;
int length;
int used;
bool solved;
};
wordDictionary allWords[MAX_WORDS];

int readFile();

void main(void)
{
int wordCount;


cout << "WELCOME TO HANGMAN....!" << endl << endl;
wordCount = readFile();
cout << wordCount;

system("pause");
}

int readFile()
{
int index = 0;
fstream myFile;


myFile.open("hangman.txt");
if( myFile.is_open() )
{
while( !myFile.eof() )

{
myFile >> allWords[index].word;
allWords[index].length = allWords[index].word.length();
allWords[index].used = false;
allWords[index].solved = false;
index++;
}
}
else
{
cout << "Can't open file ";
}
for( int i = 0; i < index; i++)
cout << allWords[i].word << " ";

return index;

cout << endl << endl;
}

These are the instructions that I have.

Programming Requirements:
1. Display title screen.

2. Read up to 50 words from a file into an appropriate data structure.

3. Randomly select a word for the game from the data structure.

4. Display the game word, letter list and the Hangman gallows.

5. Allow the user to choose a letter and mark the letter as used.

6. If the letter is in the word, reveal the letter in the game word. If the letter is not in the word,
add a body part to the gallows.

7. Track the number of guesses in the game. Once the player has missed a total of 6 letters the
game is over (gallows should display a full body) and a losing message should be displayed.

8. If the word is fully revealed before 6 guesses the player should be congratulated for the win.


9. Following either a win or lose the player is prompted to play another game.

10. If the player chooses to play again, the game selects a different word reinitializes the display
(empty gallows, all letters are displayed, game word is represented by "_" or other such
characters).

11. If the player chooses not to play again display a credit screen.


Any help would be greatly appreciated!

Last edited on
I have the same assignment It's due today I think, and I'm pretty sure that I'm screwed. I cant figure it out, but Good luck to you. You may be fortunate enough to get it.
Last edited on
Okay, so here's what you can do..
1) You can select a random word using the rand() function...
2) When the user enters a letter, you search the game word for the letter, using either a custom string search function, or strtok... Also, have an array of all the 26 letters, say char alpha[26], having alpha[1]=a; alpha[2]=b;, and so on... use the tolower function in case the user has CAPSLOCK on...
When the user enters a letter that is there in the game word, access that array element and replace it with a "_"...
3) You can make a function, that checks for the number of wrong guesses till the point, and accordingly prints some ASCII Hangman pic(Use your imagination!)... Have 6 levels of this pic, depending on number of wrong guesses..
4) Put the entire game in a do-while loop... Ask whether the user wants to play again, and accordingly re-iterate or quit the loop.
5) Keep a printnext kind of function which scans the progress of the game after each input of letter, and accordingly prints the next screen (for example, strike out the guessed letter, print the next level of the hangman gallows if wrong guess, etc...)

Hope this helps in some way... :)
did you get it to work?
Topic archived. No new replies allowed.