weziw 0 Newbie Poster

Could someone please help me with my code for the hangman game. The problem seems to be with the playGame() functions while loop.

#include <iostream>
#include <fstream.h>
#include <cstdlib>
#include <cstring>
#include <ctime>		//To generate a random seed	

		//The function prototypes

void DrawState(int state);			//Draw the hanging progress steps
void getWords();					//Get the word for user to guess
void playGame();					//Play the game


	//Global variables

const int numWords = 5;				//The number of words in the games vocabulary
//const int numLetter = 15;			//Maximum word length
const int numLetter = 15;
typedef char  aWord[numLetter];
aWord *wordList;


	//The main function	

int main(int argc, char * argv[])
{
	char Play = 'Y';

	cout << "\n\tWelcome to the hangman game\n"
		 << "\n\t===========================\n";
	cout << "You have five tries to save the poor soul from hanging\n";
	cout << "All words are up to a maximum of 15 letters long\n\n";

	//Get words from the text file
	getWords();

	//Play the game
    while(toupper(Play) == 'Y')
	{
		playGame();
		cout << "Play again? (Y or N) ";
		cin  >> Play;
	}


	return 1;
}//end main


//This function loads the words into an array of C-strings from a file
//located in the same directory. getWords() is called by main()

void getWords()
{
	ifstream wordFile;
	const int numLetter = 15;
	const int numWords = 10;
	int i = 0, count = 0;
	char fileName[30], oneWord[15];

	cout << "Enter the name of the file: ";
	cin  >> fileName;
	wordFile.open(fileName);

	//Count the number of words in the file
	while(!wordFile.eof())
	{
		wordFile >> oneWord;
		count++;
	}
	
	//Dynamically allocate an array
	wordList = new aWord[count];
	
	//Reset the file to read from the begining
	wordFile.clear();
	wordFile.close();
	wordFile.open(fileName);

	//Read all the words into the array of words
	while(!wordFile.eof())
	{
		wordFile>>wordList[i++];
	}

	//Show the word file contents as a check (Delete this part)
	for(i=0;i<count;i++)
	{
		cout << wordList[i] << "\n";
	}

	wordFile.close();

}//end getWord


//Function to draw the program states after each guess. The 
//function uses if statements to decide which state of the game
//to draw depending on how many times a bad guess is entered.
//The user has up to 5 chances and on the sixth wrong guess, the 
//player loses the game.
//This function is called by playGame()

void DrawState(int State)
{
	if(State==6)
	{
  
	  cout<<endl<<endl
		  <<"   +----+      "<<"\n"
		  <<"   |    |      "<<"\n"
		  <<"   |    O      "<<"\n"
		  <<"   |   /|\\    "<<"\n"
		  <<"   |   / \\    "<<"\n"
		  <<"   |Your Dead! "<<"\n"
		  <<"  ============ "<<"\n\n";
	}//end if state 6

	if(State==5)
	{
  
	  cout<<endl<<endl
		  <<"   +----+     "<<"\n"
		  <<"   |    |     "<<"\n"
		  <<"   |    O     "<<"\n"
		  <<"   |   /|\\   "<<"\n"
		  <<"   |   /	   "<<"\n"
		  <<"   |		   "<<"\n"
		  <<"  ============"<<"\n\n";
	}//end if state 5

	if(State==4)
	{
  
	  cout<<endl<<endl
		  <<"   +----+     "<<"\n"
		  <<"   |    |     "<<"\n"
		  <<"   |    O     "<<"\n"
		  <<"   |   /|\\   "<<"\n"
		  <<"   |	       "<<"\n"
		  <<"   |		   "<<"\n"
		  <<"  ============"<<"\n\n";
	}//end if state 4

	if(State==3)
	{
  
	  cout<<endl<<endl
		  <<"   +----+     "<<"\n"
		  <<"   |    |     "<<"\n"
		  <<"   |    O     "<<"\n"
		  <<"   |   /|     "<<"\n"
		  <<"   |		   "<<"\n"
		  <<"   |		   "<<"\n"
		  <<"  ============"<<"\n\n";
	}//end if state 3

	if(State==2)
	{
  
	  cout<<endl<<endl
		  <<"   +----+     "<<"\n"
		  <<"   |    |     "<<"\n"
		  <<"   |    O     "<<"\n"
		  <<"   |    |     "<<"\n"
		  <<"   |		   "<<"\n"
		  <<"   |		   "<<"\n"
		  <<"  ============"<<"\n\n";
	}//end if state 2

	if(State==1)
	{
  
	  cout<<endl<<endl
		  <<"   +----+     "<<"\n"
		  <<"   |    |     "<<"\n"
		  <<"   |          "<<"\n"
		  <<"   |		   "<<"\n"
		  <<"   |		   "<<"\n"
		  <<"   |		   "<<"\n"
		  <<"  ============"<<"\n\n";
	}//end if state 1

}//end Draw

/*
  This functions main purpose is to compare the user guesses letter
  by letter to each letter in the actual randomly chosen word. It gives
  the player a clue by showing the user how long the word is and also, 
  for each correct guess, the correct letter is placed in the correct
  position as it would be in the final completed word. If
*/

void playGame()
{
	int wordPos;			//word position in the array of words
	int wordLen;			//Length of the word chosen
	int State = 1;			//The game stage
	int letterPos = 0;		//The position of the letter in the word
	char guess[numLetter]; //The user guess
	char wCopy[numLetter]; //Copy of the actual word
	char letter;			//User attempt
	typedef bool flag;		//Define flag as a boolean variable
	flag good = false;		//good is of type flag to flag the user guesses

	srand(unsigned int(time(0)));		//Set the seed with time
	wordPos = rand() % numWords;		//Choose a random word

	strcpy(wCopy,wordList[wordPos]);	//Copy the word into a temporary variable for comparison
	wordLen = strlen(wCopy);			//Get the size of the word


	//Initialise the guess array with the same number of dashes as the 
	//actual word length and terminate with a null terminator
	for(letterPos =0;letterPos<wordLen;letterPos++)
	{
		guess[letterPos] = '-';
	}
	guess[letterPos] = '\0';

	//Now select the current state of the hanging progress
	//and draw it to the screen
	
	DrawState(State);			//Show the current state
    cout << guess << "\n\n";	//Current guess state

	//Get the users attempt
	cout << "Enter a letter: ";
	cin  >> letter;

	for(letterPos = 0; letterPos< wordLen; letterPos++)
	{
		if(letter == wCopy[letterPos]) //First if
		{
			guess[letterPos] = letter;
			good =true;
			cout << "Safe for the moment";

			if( strcmp(wordList[wordPos],wCopy) == 0)
			{
				cout << "You're a winner!";
				return;
			}//end second if

		}//end first if

	}//end for

    if(!good)
	{
		cout << "That was not a  good guess";
		State++;
	}//end if not good

	good = false;		//reset the flag

	//Draw an update of the hangman progress
	DrawState(State);

	cout << "The word was " << wordList[wordPos] <<"\n\n";

}//end of playGame
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.