Thread: String - Hangman Simulation

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    String - Hangman Simulation

    I'm looking for help in how to properly modify my program so that a display message will indicate the number of incorrect guesses that remain each time the user enters an incorrect letter. This is for a program that simulates the Hangman game. Could anyone offer help or suggestions?

    Code:
    #include <iostream>
    #include <string>
    using namespace  std;
    
    int main()
    {
    	//declare the variables
    	string origWord = "";
    	string letter = "";
    	char dashReplaced ='N';
    	char gameOver ='N';
    	int numIncorrect = 0;
    	string displayWord = "-----";
    
    	//get original word
    	do //begin loop
    	{
    		cout << "Enter a 5-letter word in uppercase: ";
    		getline(cin, origWord);
    	}   while (origWord.length() != 5);
    	//clear the system
    	system("cls");
    
    	//start guessing
    	cout << "Guess this word: " <<
    	displayWord << endl;
    	while (gameOver == 'N')
    	{
    		cout << "Enter an uppercase letter: ";
    		cin >> letter;
    
    		//serarch for the letter in the original word
    		for (int x = 0; x < 5; x += 1)
    		{
    			//if the current character matches
    			//the letter, replace the corresponding
    			//dash in the displayWord variable and then
    			//set the dashReplaced variable to 'Y'
    			if (origWord.substr(x, 1) == letter)
    			{
    				displayWord.replace(x, 1, letter);
    				dashReplaced = 'Y';
    			}   //end if
    		}   //end for
    
    		//if a dash was replace, check whether the
    		//displayWord variable contains any dashes
    		if (dashReplaced == 'Y')
    		{
    			//if the displayWord variable does not
    			//contain any dashes, the game is over
    			if (displayWord.find("-", 0) == -1)
    			{
    				gameOver = 'Y';
    				cout << endl << "Yes, the word is "
    					<< origWord << endl;
    				cout << "Great guessing!" << endl;
    			}
    			else //otherwise, continue guessing
    			{
    				cout << endl << "Guess this word: "
    					<< displayWord << endl;
    				dashReplaced = 'N';
    			} //end if
    		}
    		else //processed when dashReplaced contains 'N'
    		{
    			//add 1 to the number of incorrect guesses
    			numIncorrect += 1;
    			//if the number of incorrect guesses is 10,
    			//the game is over
    			if (numIncorrect == 10)
    			{
    				gameOver = 'Y';
    				cout << endl << "Sorry, the word is "
    					<< origWord << endl;
    			} //end if
    
    		} //end if
    
    	} //end while
    
    	system("pause");
    	return 0;
    } //end of main function

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So ... you're calculating the number of incorrect guesses. You say you want to print it out. You print out many many other things along the way. What's different about this one?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. having trouble splitting a main file into 2 classes.
    By lorenzo86 in forum C++ Programming
    Replies: 12
    Last Post: 01-16-2010, 09:46 PM
  2. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM