// Hangman Redo Program
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

//Functions
void welcome();
void instructions();
int game(string sofar, const string THE_WORD, int MAX_WRONG, int wrong, string used);

// Types and Arrays
string choice;
string inst;
const int MAX_WRONG = 8;

int main()
{
	vector<string>words;
	words.push_back("ALPHABET");
	words.push_back("SKUNK");
	words.push_back("ANIMALS");
	words.push_back("TABLE");
	words.push_back("CASE");
	words.push_back("MONITOR");
	words.push_back("TELEVISION");
	words.push_back("SOAP");
	words.push_back("BERUIT");
	words.push_back("WRAP");
	words.push_back("CHAIR");
	words.push_back("CHARTER");
	words.push_back("COMPUTER");
	words.push_back("SHARPENER");
	words.push_back("PENCIL");
	words.push_back("WALLS");
	words.push_back("LUNG");
	words.push_back("NOTHING");
	words.push_back("HAIR");
	words.push_back("SECRUITY");
	words.push_back("NIXON");
	words.push_back("DESTROY");
	words.push_back("OUTLET");

			srand(static_cast<unsigned int>(time(0)));
	random_shuffle(words.begin(), words.end());
	const string THE_WORD = words[0];
	int wrong = 0;
	string sofar (THE_WORD.size(), '-');
	string used = "";

		welcome();

	return 0;
}
void welcome()
{
		for (int f = 0; f < 11; f++) // formating
		{
			cout << endl;
		}

	cout << "\t\tWelcome to Hangman 2.0 By Khoanyneosr!";

		for (int i = 0; i < 14; i++) // for statement to create space under title
		{
			cout << endl;
		}
			system("pause"); // creates a pause;

			system("cls"); // clears the console screen
		cout << endl << "Would you like to see the instructions?\n\n";
		cout << endl << "Yes - Y" << endl;
		cout << endl << "No - N\n\n- ";
			cin >> choice;
		
		transform(choice.begin(), choice.end(), choice.begin(), toupper); // toupper algorithm
		cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
	
bool done = false;
	while (done == false)
	{
		

		if (choice == "Y")
		{
			instructions();
			done = true;
			break;
		}
		else if (choice == "N")
		{
			game();
			done = true;
			break;
		}
		else
		{
			cout << endl << "Invalid input.\n- ";
			cin.clear();
			cin >> choice;

		transform(choice.begin(), choice.end(), choice.begin(), toupper); // toupper algorithm
		cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' ); // limit cin to one character.

		}
	};


}
void instructions()
{
	system("cls");
	cout << endl << "\t\tINSTRUCTIONS: ";
	cout << endl << "1 - You'll be presented with some blank spaces.\n";
	cout << endl << "2 - Your job is to guess which letters go in the spaces.\n";
	cout << endl << "3 - If you guess the right letter, then you'll be given 1 point.\n";
	cout << endl << "4 - If you guess the wrong letter 1 point will be deducted.\n";
	cout << endl << "5 - If you think that you know the word, you can guess the word\n\n\tby typing \"Guess\" at any time.\n\n";
	cout << endl << "Good Luck!" << endl << endl;
	system("pause");
	game();
}
int game(string sofar, const string THE_WORD, int MAX_WRONG, int wrong, string used)
{
	system("cls");
		while ((wrong < MAX_WRONG) && (sofar != THE_WORD))
		{
			cout << "\n\nYou have " << (MAX_WRONG - wrong);
			cout << " incorrect guesses left.\n";
			cout << "\nSo far, the word is:\n" << sofar << endl;

			char guess;
			cout << "\n\nEnter a guess: ";
			cin >> guess;
			cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' ); // limit cin to one character.
			guess = toupper(guess);
			while (used.find(guess) != string::npos)
			{
				cout << "\nYou've already guessed " << guess << endl;
				cout << "Enter your guess: ";
				cin >> guess;
				guess = toupper(guess);
			}

			used += guess;

			if (THE_WORD.find(guess) != string::npos)
			{
				cout << "That's right! " << guess << " is in the word.\n";
			
				// update the newly guessed letter into the word
				for (unsigned int i = 0; i < THE_WORD.length(); ++i)
				{
					if (THE_WORD[i] == guess)
					{
						sofar[i] = guess;
					}
				}
			}
			else
			{
				cout << "Sorry, " << guess << " isn't in the word.\n";
				++wrong;
			}
		}

		// shut down
		if (wrong == MAX_WRONG)
		{
			cout << "\nYou've been hanged!";
		}
		else
		{
			cout << "\nThe word was " << THE_WORD << endl;
		}
	return 1;	
}

So that's my code as of now. I originally had it as void and returning no values but the compiler said...


Error 1 error C2660: 'game' : function does not take 0 arguments c:\users\konnor\documents\visual studio 2010\projects\hangman_redo\hangman_redo\hangman_redo.cpp 97 1 hangman_redo

so.... I changed it to int and returning 1. The same thing happened. Im not sure what to do from here so... please help =)

In the instructions function and in the welcome function, you're calling game() without any arguments, when the function is stated to take 5.

In the instructions function and in the welcome function, you're calling game() without any arguments, when the function is stated to take 5.

I took all of my code and did this to the game() function:

game(string sofar, const string THE_WORD, int MAX_WRONG, int wrong, string used);

but now the "string sofar" is underlined in red and it says its a type "C2275 ERROR"

Is that the function declaration or the function call? If it's the call, get rid of the types(string, int).

Line 54: string used = "";
string used is also a function argument. This is a conflict because you are also declaring a local variable string named "used".

Hey guys, none of that worked. Would you mind putting your examples in the {code} things? I'm not sure im understanding it correctly. Thanks

Function prototype

int game(string sofar, const string THE_WORD, int MAX_WRONG, int wrong, string used);

Function call

game(sofar, THE_WORD, MAX_WRONG, wrong, used);

You can't replace lines 97 and 126 with this:

game(string sofar, const string THE_WORD, int MAX_WRONG, int wrong, string used);

It has to look something like this:

game(sofar, THE_WORD, MAX_WRONG, wrong, used);

assuming those are the right variables. Function calls don't have the types. Function prototypes do.

commented: Good clarity and detail. +1

Function prototype

int game(string sofar, const string THE_WORD, int MAX_WRONG, int wrong, string used);

Function call

game(sofar, THE_WORD, MAX_WRONG, wrong, used);

You can't replace lines 97 and 126 with this:

game(string sofar, const string THE_WORD, int MAX_WRONG, int wrong, string used);

It has to look something like this:

game(sofar, THE_WORD, MAX_WRONG, wrong, used);

assuming those are the right variables. Function calls don't have the types. Function prototypes do.

Thanks man, now it makes sense. Also it fixed my problem =)

Bad news! it didn't work. I wrote the last thing as it was compiling, and it didn't compile... URGH! Heres more of my code (i'll cut out whats not needed)

//Functions
void welcome();
void instructions();
int game(string soFar, const string THE_WORD, int MAX_WRONG, int wrong, string used);

// Types and Arrays
string choice;
string inst;
const int MAX_WRONG = 8;

int main()
{
	vector<string>words;
	words.push_back("ALPHABET");

			srand(static_cast<unsigned int>(time(0)));
	random_shuffle(words.begin(), words.end());
	const string THE_WORD = words[0];
	int wrong = 0;
	string soFar (THE_WORD.size(), '-');
	string used = "";

		welcome();

	return 0;
}
void welcome()
{
		for (int f = 0; f < 11; f++) // formating
		{
			cout << endl;
		}

	cout << "\t\tWelcome to Hangman 2.0 By Khoanyneosr!";

		for (int i = 0; i < 14; i++) // for statement to create space under title
		{
			cout << endl;
		}
			system("pause"); // creates a pause;

			system("cls"); // clears the console screen
		cout << endl << "Would you like to see the instructions?\n\n";
		cout << endl << "Yes - Y" << endl;
		cout << endl << "No - N\n\n- ";
			cin >> choice;
		
		transform(choice.begin(), choice.end(), choice.begin(), toupper); // toupper algorithm
		cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
	
bool done = false;
	while (done == false)
	{
		

		if (choice == "Y")
		{
			instructions();
			done = true;
			break;
		}
		else if (choice == "N")
		{
			game(soFar, THE_WORD, MAX_WRONG, wrong, used);
			done = true;
			break;
		}
		else
		{
			cout << endl << "Invalid input.\n- ";
			cin.clear();
			cin >> choice;

		transform(choice.begin(), choice.end(), choice.begin(), toupper); // toupper algorithm
		cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' ); // limit cin to one character.

		}
	};


}
void instructions()
{
	system("cls");
	cout << endl << "\t\tINSTRUCTIONS: ";
	cout << endl << "1 - You'll be presented with some blank spaces.\n";
	cout << endl << "2 - Your job is to guess which letters go in the spaces.\n";
	cout << endl << "3 - If you guess the right letter, then you'll be given 1 point.\n";
	cout << endl << "4 - If you guess the wrong letter 1 point will be deducted.\n";
	cout << endl << "5 - If you think that you know the word, you can guess the word\n\n\tby typing \"Guess\" at any time.\n\n";
	cout << endl << "Good Luck!" << endl << endl;
	system("pause");
	game(soFar, THE_WORD, MAX_WRONG, wrong, used);
}
int game(soFar,THE_WORD, MAX_WRONG, wrong, used)
{
	system("cls");
		while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
		{
			cout << "\n\nYou have " << (MAX_WRONG - wrong);
			cout << " incorrect guesses left.\n";
			cout << "\nSo far, the word is:\n" << soFar << endl;

			char guess;
			cout << "\n\nEnter a guess: ";
			cin >> guess;
			cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' ); // limit cin to one character.
			guess = toupper(guess);
			while (used.find(guess) != string::npos)
			{
				cout << "\nYou've already guessed " << guess << endl;
				cout << "Enter your guess: ";
				cin >> guess;
				guess = toupper(guess);
			}

			used += guess;

			if (THE_WORD.find(guess) != string::npos)
			{
				cout << "That's right! " << guess << " is in the word.\n";
			
				// update the newly guessed letter into the word
				for (unsigned int i = 0; i < THE_WORD.length(); ++i)
				{
					if (THE_WORD[i] == guess)
					{
						soFar[i] = guess;
					}
				}
			}
			else
			{
				cout << "Sorry, " << guess << " isn't in the word.\n";
				++wrong;
			}
		}

		// shut down
		if (wrong == MAX_WRONG)
		{
			cout << "\nYou've been hanged!";
		}
		else
		{
			cout << "\nThe word was " << THE_WORD << endl;
		}

		return 0;
}

im not sure where i went wrong...

It may be helpful to post the error output of the compiler...

You have scoping issues. There's no soFar variable in the instructions() function, but you have it in the call. Ditto THE_WORD. You declare them in main, so they can't be seen in instructions(). If you make them global instead, instructions() will be able to see them.

Should you? I don't know. You'll have to go through the program and decide what functions need what parameters and why. If it was my program, I probably would move some of the code from "welcome" and "instructions" into main and call the game function from main. That's just my way of organizing it. It's not the only way. But I think a step back is required as far as the overall program design. Figure out what functions need and what functions do. If scoping is a brand new concept, perhaps set the assignment aside and familiarize yourself with it, then come back.

try const with the types

int game(const string &soFar, const string &THE_WORD, const int &MAX_WRONG,const int &wrong,const string &used);

You have mixed them up. const variable into a non const method values.

Just noticed this. In addition to my last comment, line 96 is incorrect. Line 96 needs to match line 5 exactly, except leave the semicolon off line 96.

The errors the compiler is getting are the...

C2065 error

and the...

C2448

Hello khoanyneosr,
I think you gave wrong function prototype for game function.
Try giving this in your function definition.

int game(string soFar, const string THE_WORD, int MAX_WRONG, int wrong, string used)
{
}

Try this as your prototype.

int game(string,const string,int,int,string);

or

Try declaring the variables soFar etc., again in the game function definition.

The errors the compiler is getting are the...

C2065 error

and the...

C2448

Makes sense. Ignore the error numbers. Focus on the error messages and the line numbers. I think you'll find they're either bad identifiers or bad types and will be on lines 94 and 96. See prior posts for why you're getting the errors. Line 96 error should go away immediately if you copy line 5 into line 96 and delete he semicolon. Line 94 is more difficult and requires some knowledge of scoping to fix.

Hey guys, Thanks for the help! I appreciate it, but there's still a problem. Now the only problem the compiler is stating is C2275 for "string soFar". I've moved the welcome function to the int main function and got rid of it... so... still not sure where to go from here.

What is the error ? It's hard to guess the error about soFar with the error number.

1>------ Build started: Project: hangman_redo, Configuration: Debug Win32 ------
1>Build started 3/18/2011 11:05:47 PM.
1>InitializeBuildStatus:
1> Touching "Debug\hangman_redo.unsuccessfulbuild".
1>ClCompile:
1> hangman_redo.cpp
1>c:\users\konnor\documents\visual studio 2010\projects\hangman_redo\hangman_redo\hangman_redo.cpp(93): error C2275: 'std::string' : illegal use of this type as an expression
1> c:\program files\microsoft visual studio 10.0\vc\include\xstring(2063) : see declaration of 'std::string'
1>c:\users\konnor\documents\visual studio 2010\projects\hangman_redo\hangman_redo\hangman_redo.cpp(93): error C2146: syntax error : missing ')' before identifier 'soFar'
1>c:\users\konnor\documents\visual studio 2010\projects\hangman_redo\hangman_redo\hangman_redo.cpp(93): error C2059: syntax error : ')'
1>c:\users\konnor\documents\visual studio 2010\projects\hangman_redo\hangman_redo\hangman_redo.cpp(123): error C2275: 'std::string' : illegal use of this type as an expression
1> c:\program files\microsoft visual studio 10.0\vc\include\xstring(2063) : see declaration of 'std::string'
1>c:\users\konnor\documents\visual studio 2010\projects\hangman_redo\hangman_redo\hangman_redo.cpp(123): error C2146: syntax error : missing ')' before identifier 'soFar'
1>c:\users\konnor\documents\visual studio 2010\projects\hangman_redo\hangman_redo\hangman_redo.cpp(123): error C2059: syntax error : ')'
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:03.07
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


that's all i got...

From your errors it is clear that you have left ')' for function call in line 93 and 123, but regarding the error std::string it will be helpful if you post lines 93 and 123.

line 93 = game(string soFar, const string THE_WORD, int MAX_WRONG, int wrong, string used);


line 123 = game(string soFar, const string THE_WORD, int MAX_WRONG, int wrong, string used);

I hope the line 123 is function declaration for definition. I think this is the problem, you have terminated the definition by giving ';' .

So, i decided to start the whole function structure from scratch and build up from there. I removed the game(//stuff in here);

And it compiled!!!! but it says a vector on line 942 or something isn't valid. Please compile and see for yourself. From the original code just remove the game(//stuff in here );

Thanks.

Well, one thing I notice in your declaration vector<string>words; is the lack of a space between ">" and the variable name "words". It should be vector<string> words; so it can be properly parsed.

Well, one thing I notice in your declaration vector<string>words; is the lack of a space between ">" and the variable name "words". It should be vector<string> words; so it can be properly parsed.

Same problem... =(

It says...


debug assertion failed!

program: ...sual studio
2010\projects\hangman_redo\debug\hangman_redo.exe
file: c:\program files\microsoft visual studio 10.0\vc]include\vector
Line: 932

expression: vector subscript out of range

for information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press retry to debug the application)
...

so when i hit debug it just says "hangman_redo.exe has stopped working"

and... that's the most specific i can be sorry

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.