Hello friends, I need some help with my c++ hangman code.The code is below.

/*Game of Hangman
	Author - Chirag Mittal */
#include<iostream.h>	// For input/output
#include<fstream.h>	// For file input/output
#include<string.h>	// For strcpy
#include<time.h>		// For time
#include<stdlib.h>
#include<ctype.h>    //for toupper & tolower

#define MAX_WORD_SIZE 15	// The max size for words
#define MAX_WORDS 255		// The max number of words

void LoadFile();	// Prototypes for our functions
void RunGame();
void DrawGallows(int State);


typedef char String[MAX_WORD_SIZE];	// Make a char type with 15 chars
String Words[MAX_WORDS - 1];		// This array will hold our words
int Count;							//word count

// the main function of our hangman game
void main()
{
	char Continue = 'Y';	// end game if = to 'N'
	cout<<"Welcome to Hangman . . . Don't lose your head!"<<endl;

	LoadFile();	// Load the data file

	// Continue the game as long as the player wants
	while(Continue == 'Y')
	{
		RunGame();	// Run the game
		cout<<endl<<"Do you want to play again (Yes or No)? ";
		cin>>Continue;
		Continue = toupper(Continue);
	}

	// say good bye
	cout<<endl<<"Thanks for playing."<<endl;
}

// This will load our file
void LoadFile()
{
	char C;				//Used to find EOF
	ifstream Datfile;	//The in file

	Count=0;	//Set count to 0

	// Open the data file
	Datfile.open("words.dat");

	//Loop till the end of file
	while((C=Datfile.peek()) != EOF)
	{
		// Get the next word and then increment Count
		Datfile>>Words[Count++];

		// If we surpass the max exit and tell the user
		if(Count > MAX_WORDS - 1)
		{
			cout<<endl<<"Too many words in the file, stopping with "
				<<MAX_WORDS<<" Words."<<endl;
			Count = MAX_WORDS;
			break;
		}

	}

	// We need to subtract one to get the correct number of words
	Count--;

	// Close the data file
	Datfile.close();

}

// This function will run the game
void RunGame()
{
	int Word;			// This will hold the subscript of our word
	int Size;			// This will hold the length of our word
	int State=1;		// This holds the game state
	int Subscript=0;	// This will hold subscripts
	char Guess[MAX_WORD_SIZE];	// this will hold their current word
	char Copy[MAX_WORD_SIZE];	// This will hold a copy of the word
	char Letter;		//This will be their letter guess
	int Correct=0;		// This is a True/False value deciding if they got a good answer
	
	// Seed and create a random number
	srand((unsigned)time( NULL ));	// use time as a seed
	Word = rand() % Count;

	// Make a local copy of the word
	strcpy(Copy,Words[Word]);

	Size = strlen(Words[Word]);	// Get the word's size

	// Create a null terminated string to represent the word as the
	// player knows it.
	for(; Subscript < Size; Subscript++)
	{
		Guess[Subscript] = '-';
	}

	// insert the null charachter
	Guess[Subscript] = '\0';

	// Go till the player is hung 
	while(State!=6)
	{
		DrawGallows(State);	//Draw the gallows
		cout<<Guess<<endl;	// Draw Guess

		cout<<"Guess a letter :";
		cin>>Letter;

		// We will use only lower case letters
		Letter = tolower(Letter);
		
		// Loop through the word
		for(Subscript = 0; Subscript < Size; Subscript++)
		{

			//if the guess is good tell the user and update Guess
			if(Copy[Subscript] == Letter)
			{
				Guess[Subscript] = Letter;
				Correct = 1;
				cout<<endl<<"Good Guess!";

				// If guess equals the word they won so exit
				if(strcmp(Words[Word],Guess) == 0)
				{
					cout<<endl<<"Yea, You survived and won!";
					return;
				}
			}
		}

		// If they didn't get aletter correct chide the user
		if(Correct == 0)
		{
			cout<<endl<<"Sorry, bad guess!";
			State++;
		}

		Correct = 0;	// reset Correct

	}

	DrawGallows(State);	//Draw the gallows at end of game
	//They lost if they are here so tell them the answer.
	cout<<"The word was : "<<Words[Word]<<endl<<endl;	

}

// This will Draw the gallows according to the state
void DrawGallows(int State)
{
	if(State==6)
	{
		// The \\ will translate as '\' because it is a special char
		cout<<endl<<endl
			<<"   +----+     "<<endl
			<<"   |    |     "<<endl
			<<"   |    O     "<<endl
			<<"   |   /|\\   "<<endl
			<<"   |   / \\   "<<endl
			<<"   |Your Dead "<<endl
			<<"  ============"<<endl<<endl;
	}
	else if(State==5)
	{
		cout<<endl<<endl
			<<"   +----+  "<<endl
			<<"   |    |  "<<endl
			<<"   |    O  "<<endl
			<<"   |   /|\\ "<<endl
			<<"   |     \\ "<<endl
			<<"   |       "<<endl
			<<"  ============"<<endl<<endl;
	}
	else if(State==4)
	{
		cout<<endl<<endl
			<<"   +----+  "<<endl
			<<"   |    |  "<<endl
			<<"   |    O  "<<endl
			<<"   |   /|\\ "<<endl
			<<"   |       "<<endl
			<<"   |       "<<endl
			<<"  ============="<<endl<<endl;
	}
	else if(State==3)
	{
		cout<<endl<<endl
			<<"   +----+  "<<endl
			<<"   |    |  "<<endl
			<<"   |    O  "<<endl
			<<"   |   /|  "<<endl
			<<"   |       "<<endl
			<<"   |       "<<endl
			<<"  ============="<<endl<<endl;
	}
	else if(State==2)
	{
		cout<<endl<<endl
			<<"   +----+  "<<endl
			<<"   |    |  "<<endl
			<<"   |    O  "<<endl
			<<"   |    |  "<<endl
			<<"   |       "<<endl
			<<"   |       "<<endl
			<<"  ============="<<endl<<endl;
	}
	else if(State==1)
	{
		cout<<endl<<endl
			<<"   +----+  "<<endl
			<<"   |    |  "<<endl
			<<"   |       "<<endl
			<<"   |       "<<endl
			<<"   |       "<<endl
			<<"   |       "<<endl
			<<"  ============="<<endl<<endl;
	}

}

The problem with this code is that it isn't taking up randomized words from the file "words.dat", instead I don't even know where is it taking up words from. There is no other file with the same name in all of my hard-disk.

Please help me to find where is my fault.

Thank you in advance.

Welcome to the Board! You're off to a good start with your first post. You figured out how to post code using code tags, you use consistent indentation, you have commented your code, and you asked a relevant question. Here are some comments:

In general:
1) The return type of main() should be int, not void.
2) You are using old header files. If your current compiler doesn't support the up to date variety----standard files no longer have the .h extension, then it's time to update to a new one. Since you have access to the internet you have access to several new, up to date, free compilers.
3) Why did you complete the entire program and then try to solve a problem very early in the code? Who knows if the rest of the code has porblems or not (looks pretty code on paper, though)? In general you would be better off compiling frequently while writing the code. I would recommend writing only a single task or small function without compiling.
4) srand() only needs to be called once during each program. Line 92 would be better placed early in main().

More to your question:
1) What type is String? I don't see it declared anywhere. If String isn't a vaild type, then Words isn't a valid variable and LoadFile has no place to store inpur from file. Given that you aren't use namespace std I would suggest declaring Word as a 2 dimenstional array of char.

char Words[MAXWORDS][MAXWORDSIZE + 1];

2) I would declare MAXWORDS and MAXWORDSIZE as type const int, not with a #define.

3) The more typical syntax for file reading would be:

while(Datfile >> Words[Count++])

The input stream, Datfile, will go into a fail state preventing it from reading beyond EOF and return zero, effectively stopping any further input. eof() can then be queried to be sure the entire file was read and the input failed for that reason instead of something else.

Thanks for the suggestions and help.
It really helped me,but there were some things that I didn't understand (I am a new bee to c++ coding),like :->
1) as you said srand() must be declared early in the main(),my question is where as I declared it in the loadfile(),and it is called early in the main()....

2) where can I get the new free compilers,as u mentioned,I looked over and over again on the internet couldn't find it anywhere...please provide links if convenient to you..

Thank you.....

Place line 92 on line 27 or anywhere else in main() before the while loop. Eventhough srand() is called in a function different from where the result of srand() is called/used, it will still work.

Thanks to all of you guys...
I was able to successfully complete my project work..
all you guys just rock!!

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.