Sign in to follow this  

I finally made Hangman xD

This topic is 3728 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

This topic is 3728 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this