Jump to content
  • Advertisement
Sign in to follow this  
sheep19

I finally made Hangman xD

This topic is 4034 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I finally made Hanagman!! It has only one word now, but I will add more. Here it is: Feel free to write your opinions or suggestions.
#include <iostream>
#include <string>

using std::cout;	using std::cin;
using std::string;

void check (string& word, char letter, int position)
{
	if (word.find(letter) != string::npos)
	{
		while (word.find(letter) != string::npos )
		{
			position = word.find (letter);
	
			word.erase (position, 1);
		}

		cout << "Correct! '" << letter << "' is in the word! ";
			
		if (word.empty() != true)
		{
				cout << word.size() << " characters to go." << "\n";
		}
		else
		{
				cout << "\n";
		}

		
	}
	else // if the letter is not in the word
	{
		cout << "Sorry, '" << letter << "' does not exist in the word.. "
			 << word.size() << " characters to go." << "\n";
	}
}

int main()
{
	const string original_word ("sheep");
	string word = original_word;

	const int NUMBER_OF_TRIES = 7;
	int number_of_tries_left = NUMBER_OF_TRIES;

	cout <<"The word has " << word.size() << " characters."
		 << "\n"
		 ;

	for (; number_of_tries_left > 0; number_of_tries_left --)
	{
		cout << "\n" << "You have " << number_of_tries_left << " tries left.";

		cout << "\n" << "Enter a letter: ";
		
		char letter;

		cin >> letter;

		int position; // the letter's position in the word

		check (word, letter, position); //check if the letter is in the word

		if (word.empty() )
		{
			cout << "\n" << "Congratulations, you found the word!! [" << original_word << "]";
			break;
		}
	}

	if (word.empty() != true)
	{
		cout << "\n" << "You lose..The word was '" << original_word << "'. Try harder next time.";
	}

	cout << "\n\n";

	system ("pause");

	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!