// file: hangman.cpp
// name: Kyle Burmark
// professor: M. Gelotte
// date: 2/26/02
// Description: This is a computer version of the game hangman, the user
// can play against a 2nd player or the computer, and he can enter how many 
// guesses he gets,
//

// Start of program

// Header file definitions

#include <iostream>				// Standard input/output
#include <string>				// String manipulation
#include <cctype>				// Character manipulation and testing
#include <fstream>				// File stream
#include <cstdlib>				// Used for random function
#include <time.h>				// Used for better random number
#include <draw.h>				// Draws hangman

using namespace std;

// Function declerations

void instruction(int& choice);		// Gives instructions and gets choice
void usergame(int i);				// Plays 2nd user game
void compgame(int i);				// Plays against computer
void test(string word, char letter, int& numwrong, string& temp, int i);		// Tests current letter and replaces starred word
void lchosen(char letter, string& letterchosen, int& check, int& chosencounter, int i);			// Checks current letter and adds it to letters chosen output if not entered already
void rnd(string& word, int i);		// Gets random word from file
void drawman(int numguess, int numwrong, int i);	// Draws hangman
inline istream& Flush(istream& stream);				// Flushes cin stream

// Start of main

int main()
{	
	int i = 0;			// Counter variable for loops
	int exit = 0;		// Main loop exit variable
	int choice;			// Users inputed choice for type of game or to exit

	// Main control loop

	do {				// while exit != 1
		system("cls"); // Clear the screen
		instruction(choice);		// Give instructions


		switch(choice)
		{
		case 1:
			usergame(i);			// Calls user game
			break;
		case 2: 
			compgame(i);			// Calls computer game
			break;
		case 3:
			cout << "Goodbye" << endl;	// Exits
			exit = 1;
			break;
		default:
			cerr << "Invalid choice - try again" << endl;    // Invalid choice erroe
		}
	} while (exit != 1);

	// End main loop

	system("pause");
	return 0;
}
// End main

// Subprograms
// ---------------------------------------------------------------------

// Instructions subprogram

void instruction(int& choice)
{	
	cout << "			-Hangman-" << endl << endl;
	cout << "		Created by Kyle Burmark" << endl << endl;
	cout << "*****************************************" << endl;
	cout << endl;
	cout << " Enter -1- to play against user" << endl;
	cout << " Enter -2- to play against computer" << endl;
	cout << " Enter -3- to quit" << endl;
	cout << endl;
	cout << "*****************************************" << endl << endl;
	
	cout << "Choice: ";		// Get user choice
	cin >> choice;

	// Bad data type check

	while (!cin)
	{
		cerr << "Invalid character" << endl
			<< "Enter again - choice: ";
		Flush(cin);
		cin >> choice;
	}

	// End check

	system("cls");
}

// End instruction

// User game subprogram

void usergame(int i)
{
	int numguess = 0;			// Number of guesses player gets
	int numwrong = 0;			// Number of wrong guesses player has so far
	int check;					// A variable to test whether to check the letter entered in the test function
	int wordcheck;				// A variable to check whether the user has entered an invalid word
	int end = 0;				// A variable to test what to output to the user and to exit the usergame loop
	int chosencounter = 0;		// A counter to tell the replace function where to put the letter chosen in the letterchosen string
	char letter;				// User inputed letter
	string word;				// Word user is trying to guess
	string temp;				// Updated output word user sees
	string letterchosen = "                         ";		// Defines length of letterchosen string

	do {				// while user puts in guesses outside of allowed range
	cout << "How many chances does the person have (4 - 10): ";
	cin >> numguess;
	} while (numguess < 4 || numguess > 10);

	cout << "Enter word 2nd user: ";
	cin >> word;


	do {				// while user inputs bad word
		wordcheck = 0;
		for (int i = 0; i < word.length(); i++)
		{
			if (!isalpha(word.at(i)))
			{
				wordcheck = 1;
			}
		}
		if (wordcheck == 1)
		{
			cout << "Invalid - Enter word again: ";
			cin >> word; 
		}
	} while (wordcheck == 1);

	temp = word;		// Sets temp string length to word string length
	
	// Replace temp with stars loop

	for (i = 0; i < word.length(); i++)
	{
		temp.replace(i, 1, 1,'*');
	}
	
	// End loop

	system("cls");
	
	// Main game loop

	do {
		drawman(numguess, numwrong, i);

		// Tests if user guessed word

		if (word == temp)
		{
			cout << endl << endl;
			cout << "You guessed it [ " << word << " ]" << endl << endl;
			system("pause");
			end = 1;
		}

		// Tests if user failed to guess word

		if (numwrong == numguess)
		{
			cout << endl << endl;
			cout << "You failed" << endl << endl;
			system("pause");
			end = 2;
		}

		// Play while above conditions aren't true

		if (end == 0)
		{
			cout << endl << endl << endl;
			cout << "Letters chosen: " << letterchosen << endl;
			cout << endl << endl << endl;
			cout << "Guesses left: " << numguess - numwrong << endl << endl; 
			cout << "    " << temp << endl << endl;
			cout << "Letter: ";
			cin >> letter;

			// Test for bad letter

			while (!isalpha(letter))
			{
				Flush(cin);
				cout << "Not a letter - enter letter: ";
				cin >> letter;
			}

			// End test

			lchosen(letter, letterchosen, check, chosencounter, i);
			
			// Test whether to check letter against correct word string

			if (check == 0)
			{
				test(word, letter, numwrong, temp, i);
			}
			else
			{
				;
			}

			// End test

			system("cls");
		}

		// End game
		
		system("cls");
	} while(end != 1 && end != 2);
	
	// End main game loop

	// Tests end variable to see what to display

	if (end == 2) 
	{
		cout << "Correct word was [ " << word << " ]" << endl << endl;
		system("pause");
	}
	if (end == 1)
	{
		cout << " ";
	}

	system("cls");
	
}

// End user game

// Start computer game
// See user game for variable and loop information

void compgame(int i)
{
	int numguess = 0;
	int numwrong = 0;
	int check;
	int end = 0;
	int chosencounter = 0;
	char letter;
	string word;
	string temp;
	string letterchosen = "                         ";
	
	do {
	cout << "How many chances do you want (4 - 10): ";
	cin >> numguess;
	} while (numguess < 4 || numguess > 10);

	rnd(word, i);		// Gets random word
	temp = word;

	for (i = 0; i < word.length(); i++)
	{
		temp.replace(i, 1, 1, '*');
	}

	system("cls");

	do {
		drawman(numguess, numwrong, i);
		if (word == temp)
		{
			cout << endl << endl;
			cout << "You guessed it [ " << word << " ]" << endl << endl;
			system("pause");
			end = 1;
		}
		if (numwrong == numguess)
		{
			cout << endl << endl;
			cout << "You failed" << endl << endl;
			system("pause");
			end = 2;
		}
		if (end == 0)
		{
			cout << endl << endl << endl;
			cout << "Letters chosen: " << letterchosen << endl;
			cout << endl << endl << endl;
			cout << "Guesses left: " << numguess - numwrong << endl << endl; 
			cout << "    " << temp << endl << endl;
			cout << "Letter: ";
			cin >> letter;

			while (!isalpha(letter))
			{
				Flush(cin);
				cout << "Not a letter - enter letter: ";
				cin >> letter;
			}

			lchosen(letter, letterchosen, check, chosencounter, i);
			
			if (check == 0)
			{
				test(word, letter, numwrong, temp, i);
			}
			else
			{
				;
			}

			system("cls");
		}
		
		system("cls");
	} while(end != 1 && end != 2);

	if (end == 2) 
		cout << "Correct word was [ " << word << " ]" << endl << endl;
		system("pause");
	if (end == 1)
		cout << endl;

	system("cls");
	
	
}

// End computer game

// Checks current letter chosen by user

void lchosen(char letter, string& letterchosen, int& check, int& chosencounter, int i)
{
	check = 0;
	
	// Check if letter is equal to any letter in letterchosen string loop

	for (i = 0; i < letterchosen.length(); i++)
	{
		if (letter == letterchosen.at(i))
		{
			check = 1;
		}
	}

	// End loop
	
	// Test if letter is already chosen

	if (check == 1)
	{
		cout << endl;
		cout << "Letter already chosen" << endl;
		system("pause");
	}

	// Puts letter in string if not chosen

	else
	{
		letterchosen.replace(chosencounter, 1, 1, letter);
		chosencounter++;
	}
	
}

// End lchosen function

// Tests whether letter is in word string

void test(string word, char letter, int& numwrong, string& temp, int i)
{
	int check2 = 0;		// Checks whether letter is in word string, = 1 if it is
	
	// Check for letter match loop

	for (i = 0; i < word.length(); i++)
	{
		if (letter == word.at(i))
		{
			temp.replace(i, 1, 1, letter);
			check2 = 1;
		}
	}

	// End loop

	// Displays "wrong letter" if user inputed bad letter

	if (check2 == 0)
	{
		cout << endl;
		cout << "Wrong letter" << endl;
		system("pause");
		numwrong++;
		
	}
}

// End test function

// Gets random word

void rnd(string& word, int i)
{
	
	int x;					// Random number to pass to word file loop
	ifstream ins;			// File stream
	
	srand(time(NULL));		// Gets better random number based on time
	x = rand()%100;			// Gets number 0 - 99 
	ins.open("words.txt");	// Opens random word file

	// Tests for bad file and gets word if not a bad file

	if (ins.fail())
	{
		cerr << "Words.txt is not in same folder as hangman.exe, " << endl
			<< "put in correct file and run again and make sure it's " << endl
			<< "called words.txt" << endl;
		system("pause");

		main();
	}
	else
	{
		for (i = 0; i < (x + 1); i++)
		{
			getline(ins, word);
		}
		
	}

	// End test

	ins.close();		// Close file
}

// End random function

// Draws hangman

void drawman(int numguess, int numwrong, int i)
{
 draw::draw()			// Data object

	// Draw loop based on number of guesses and number of wrong guesses

	for (i = 0; i <= numwrong; i++)
	{
		if (numguess ==  4)
		{
			switch(i)
			{
			case 1:
				d.rope();
				cout << endl;
				d.head();
				cout << endl;
				d.neck();
				break;
			case 2:
				cout << endl;
				d.leftarm();
				d.rightarm();
				break;
			case 3:
				cout << endl;
				d.waisttop();
				break;
			case 4:
				cout << endl;
				d.leftleg();
				d.rightleg();
				cout << endl << endl << "Dead" << endl;
				break;
			}		
		}
		else if (numguess == 5)
		{
			switch(i)
			{
			case 1:
				d.rope();
				break;
			case 2:
				cout << endl;
				d.head();
				cout << endl;
				d.neck();
				break;
			case 3:
				cout << endl;
				d.leftarm();
				d.rightarm();
				break;
			case 4:
				cout << endl;
				d.waisttop();
				break;
			case 5:
				cout << endl;
				d.leftleg();
				d.rightleg();
				cout << endl << endl << "Dead" << endl;
				break;
			}
		}
		else if (numguess == 6)
		{
			switch(i)
			{
			case 1:
				d.rope();
				break;
			case 2:
				cout << endl;
				d.head();
				cout << endl;
				d.neck();
				break;
			case 3:
				cout << endl;
				d.leftarm();
				d.rightarm();
				break;
			case 4:
				cout << endl;
				d.waisttop();
				break;
			case 5:
				cout << endl;
				d.leftleg();
				break;
			case 6:
				d.rightleg();
				cout << endl << "Dead" << endl;
				break;
			}
		}
		else if (numguess == 7)
		{
			switch(i)
			{
			case 1:
				d.rope();
				break;
			case 2:
				cout << endl;
				d.head();
				cout << endl;
				d.neck();
				break;
			case 3:
				cout << endl;
				d.leftarm();
				break;
			case 4:
				d.rightarm();
				break;
			case 5:
				cout << endl;
				d.waisttop();
				break;
			case 6:
				cout << endl;
				d.leftleg();
				break;
			case 7:
				d.rightleg();
				cout << endl << "Dead" << endl;
				break;
			}
		}
		else if (numguess == 8)
		{
			switch(i)
			{
			case 1:
				d.rope();
				break;
			case 2:
				cout << endl;
				d.head();
				break;
			case 3:
				cout << endl;
				d.neck();
				break;
			case 4:
				cout << endl;
				d.leftarm();
				break;
			case 5:
				d.rightarm();
				break;
			case 6:
				cout << endl;
				d.waisttop();
				break;
			case 7:
				cout << endl;
				d.leftleg();
				break;
			case 8:
				d.rightleg();
				cout << endl << "Dead" << endl;
				break;
			}
		}
		else if (numguess == 9)
		{
			switch(i)
			{
			case 1:
				d.rope();
				break;
			case 2:
				cout << endl;
				d.head();
				break;
			case 3:
				cout << endl;
				d.neck();
				break;
			case 4:
				cout << endl;
				d.leftarm();
				break;
			case 5:
				d.rightarm();
				break;
			case 6:
				cout << endl;
				d.waisttop();
				break;
			case 7:
				break;
			case 8:
				cout << endl;
				d.leftleg();
				break;
			case 9:
				d.rightleg();
				cout << endl << "Dead" << endl;
				break;
			}
		}
		else if (numguess == 10)
		{
			switch(i)
			{
			case 1:
				d.rope();
				break;
			case 2:
				cout << endl;
				d.head();
				break;
			case 3:
				cout << endl;
				d.neck();
				break;
			case 4:
				cout << endl;
				d.leftarm();
				break;
			case 5:
				d.rightarm();
				break;
			case 6:
				cout << endl;
				d.waisttop();
				break;
			case 7:
				break;
			case 8:
				cout << endl;
				d.leftleg();
				break;
			case 9:
				d.rightleg();
				cout << "One last chance. What do you want on your tombstone?";
				break;
			case 10:
				cout << endl << "Dead" << endl;
				break;
			}
		}
	}

	// End draw loop
	
}

// End draw function

// Flushes cin stream

inline istream& Flush(istream& stream)
{
	stream.clear();
	int chars_to_skip = stream.rdbuf()->in_avail();
	return stream.ignore(chars_to_skip);
}

// End flush function

// End subprogams

// End program
// file: draw.cpp
// Draw class implementation

#include "draw.h"

#include <iostream>
using namespace std;

// Constructor
draw::draw()
{
}

// Functions

// rope
void draw::rope()
{
	cout << "  |";
}
// head
void draw::head()
{
	cout << "  O";
}
// neck
void draw::neck()
{
	cout << "  |";
}
// left arm
void draw::leftarm()
{
	cout << " /";
}
// right arm
void draw::rightarm()
{
	cout << " \\";
}
// top waist
void draw::waisttop()
{
	cout << " { }";
}
// left leg
void draw::leftleg()
{
	cout << " /";
}
// right leg
void draw::rightleg()
{
	cout << " \\";
}
// file: draw.h
// Counter class definition

#ifndef DRAW_H		// Check for class redefinition
#define DRAW_H

class draw
{
	public:
		
		// Constructor
		draw();

		// Member functions
		
		// Rope
		void rope();

		// Head
		void head();

		// Neck
		void neck();

		// Left arm
		void leftarm();

		// Right arm
		void rightarm();

		// Waist top
		void waisttop();

		// Left leg
		void leftleg();

		// Right leg
		void rightleg();

}; 
#endif // DRAW_H

Sorry for keeping creating those new topic, but I am in urgent
I can not draw the hangman ,and the error is at

draw::draw()			// Data object

it announced draw has not been declared, but I included everything ,rite?

Sorry for keeping creating those new topic, but I am in urgent
I can not draw the hangman

Don't apologize, just don't do it. That's five, count 'em five, threads on this topic. Mark four of them solved. You've had comments on the other threads which you have not responded to, so are you reading them or not? If you don't understand a response, ask for clarification. If you think the thread is going nowhere, mark it solved before starting a new one at the very least.

Please correct for me on this thread. Thank you. Next time I will never repeat my mistake.. .

Please correct for me on this thread. Thank you. Next time I will never repeat my mistake.. .

Mark the other four solved (there is a "Mark As Solved" hyperlink at the bottom of the thread. Click it.) and I'll look at it.

I did , please help me

I did , please help me

#include <draw.h>

This should probably have quotes instead of brackets since you wrote it; it's not something that is part of the C++ standard.

#include "draw.h"  // use quotes, not brackets
void drawman(int numguess, int numwrong, int i)
{
 draw::draw()			// Data object

	// Draw loop based on number of guesses and number of wrong guesses

	for (i = 0; i <= numwrong; i++)
	{
		if (numguess ==  4)
		{
			switch(i)
			{
			case 1:
				d.rope();
				cout << endl;
				d.head();
				cout << endl;
				d.neck();
				break;

Line 3 - Is this supposed to be a call to the constructor? What is d in lines 14, 16, 18? If d is supposed to be an object of type draw, you probably want this in line 3:

draw d; // creates a new object of type draw called d.
void drawman(int numguess, int numwrong, int i)
{

 draw::draw();          // Data object
 draw d; // creates a new object of type draw called d.
    // Draw loop based on number of guesses and number of wrong guesses

    for (i = 0; i <= numwrong; i++)
    {
        if (numguess ==  4)
        {
            switch(i)
            {
            case 1:
                rope();
                cout << endl;
                head();
                cout << endl;
                d.neck();
                break;[/CODE]

is this what you mean, but now the program said that
'rope' undeclared, but I did in the .h file rite?

Sorry, I read only the last 3 posts - but shouldn't it be d.rope(); as well as d.head(); ?

And you don't want draw::draw() , as VernonDozier already said. Just draw d;

is this what you mean, but now the program said that
'rope' undeclared, but I did in the .h file rite?

You need to repost the line with the error. The whole line, not part. Highlight it or something. I see where it is, but point it out.

REPLACE line 3, don't add my line and keep yours. Delete the draw::draw () line.

Sorry, I read only the last 3 posts - but shouldn't it be d.rope(); as well as d.head(); ?

He had d.rope () and d.head () already, which is correct. Se7Olutionyg, did you change it? If you did, and it looks like you did, change it back. It was right before.

commented: Your patience with this guy is incredible... +12

Help me please, I fixed all of you said, and it seems that we have only the linking the error need to be fixed.
[Linker error] undefined reference to `draw::draw()'
[Linker error] undefined reference to `draw::rope()'
[Linker error] undefined reference to `draw::rightarm()'

commented: put some effort in it -2

Read Vernon's previous post instead of begging for help:

Help me please

That's just pathetic. Did you change the things Vernon mentioned?
If no: do it and do it now.
if yes: What are your new problems and show some code. Also learn how to use code-tags correctly

Help me please

What do you think my last posts were? If you made the two corrections I suggested and only those two corrections, it should compile. If it doesn't, say what's wrong, not just "Help me please".

I assume that draw is not a reference class..So you must declare it first..Example

draw p_draw = new draw;

[Linker error] undefined reference to `draw::draw()'
[Linker error] undefined reference to `draw::rope()'
[Linker error] undefined reference to `draw::rightarm()'

because I were not announced for that, sorry. I fixed all this error you said, and it seems we have the linker error

I don't know what do you mean? can you specify it in my code, some people said that, if I correct the 2 mistakes , it will compile, and it seems that it is true. however, only the linker problem

Then you can call anything you need..But private..Example..

draw *p_draw = new draw;
p_draw->rope;
//...

I don't know what do you mean? can you specify it in my code, some people said that, if I correct the 2 mistakes , it will compile, and it seems that it is true. however, only the linker problem

See post 6. Replace the brackets with quotes in this line:

#include <draw.h>

Replace:

draw::draw ();

with:

draw d;

in the line I specified. Do nothing else. It should compile, link, and run. It did for me. If it doesn't for you, there may be a problem with your makefile or something, if you are creating your own, or if you are using an IDE, perhaps you didn't create the project correctly. The code itself looks fine to me with those two corrections.

I am sorry, but does it relate to the linker error? because I am so tired now coding for the whole day. what you mean is I have to change all draw fucntion rite?

I am sorry, but does it relate to the linker error? because I am so tired now coding for the whole day. what you mean is I have to change all draw fucntion rite?

No, I mean change those two lines exactly as I suggested, don't change anything else, and the code will compile, link, and run. If it doesn't there is another problem which cannot be solved by looking at the code you posted. Change those lines and you shouldn't get linker errors.

I saved the .h file as DRAW.h , so I have to declare it in the main function is " include " draw.h" or DRAW.h , I tried both, but the program said :
draw.h : no such file or directory

I saved the .h file as DRAW.h , so I have to declare it in the main function is " include " draw.h" or DRAW.h , I tried both, but the program said :
draw.h : no such file or directory

Rename the file draw.h and put this line (which is what you have already, or should have):

#include "draw.h"

Maybe..It's intention is..

#include<draw.h>//old
#include"draw.h"//new

you should try it..

if you saved your file as DRAW.h then #include "DRAW.h" will work, unless you placed the file in a random folder of yours. In that case, you'll need to enther the full path (e.g. #include "/home/Se70lutionyg/programming/headers/DRAW.h" Edit: Sorry guys you are so fast you posted twice while I was writing this :P

I did all the things you mentioned above,
20 C:\Documents and Settings\QuynhlNguyen\Desktop\Final\HANGMAN.CPP In file included from C:\Documents and Settings\QuynhlNguyen\Desktop\Final\HANGMAN.CPP
41:2 C:\Documents and Settings\QuynhlNguyen\Desktop\Final\draw.h #endif without #if
but it still not run,
I think the problem may be in the draw.h files ,may be I should
// Counter class definition

change from
#define DRAW_H
to
#define draw_h
I tried both , but the same problem still occured

Here are the files I used to create the project and compile. I don't know what IDE you are using, but consider deleting your current project and creating a new one with the three files I uploaded. That'll solve any capitalization problems or path problems. Create a project and add the three files one at a time, then try again. You could have the files in the right place, but they may not be part of the project. I don't know. Give it a try.

Add this on the top

#if !defined DRAW_H
//...

Add this on the top

#if !defined DRAW_H
//...

Actually you mean something like :

#ifndef DRAW_H
#define DRAW_H

//code here

#endif

If you're not going to define DRAW_H, the ifndef would be a bit useless dont you think? ;)

to Vernon : I am very appriciative for your help
I tried to create a new project and add 3 stuff like you said,
but the same problem still occur. you can see on the screen ?? I take the photos and post it for you.
Thank you

I am using the Dev C++ which compiled normally with the other program

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.