4 Replies - 489 Views - Last Post: 20 November 2009 - 12:30 AM Rate Topic: -----

#1 Ragorism  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-September 09

Incorrect output

Posted 19 November 2009 - 10:22 PM

For some reason this hangman simulation program isn't giving me the correct output. I'm not certain where the root of the problem is, exactly.

If anyone could point out what it is/what it's missing, that would be fantastic. :)

For example, if I input the word 'apple', then guess that 'e' is a letter, it tells me that 'EEEEE' is the correct word.

#include <iostream>
#include <algorithm>
#include <string>

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

int main()
{
	//declare variables 
	string origWord = "";
	string letter = "";
	char dashReplaced = 'F';
	char gameOver = 'F';
	int numIncorrect = 0;
	string guessWord = "-----";
	
	//get the original word from player 1
	do
	{
		  cout << "Enter the original word: ";
		  getline(cin, origWord);
	} while(origWord.length() != 5);
	
	//convert the original word to uppercase
	transform(origWord.begin(), origWord.end(),
		 origWord.begin(), toupper);
		 
	//clear the contents of the Command Prompt window,
	//then display the five dashes in the guessed word 
	system("cls");
	cout << "Guess this word: " << guessWord << endl;
	
	//allow player 2 to guess a letter
	//the game is over when either the word is guessed
	//or player 2 makes 10 incorrect guesses
	while (gameOver == 'F')
	{
		  //get a letter from player 2, then convert the
		  //letter to uppercase
		  cout << "Enter a letter: ";
		  cin >> letter;
		  transform(letter.begin(), letter.end(), 
			  letter.begin(), toupper);
			  
		  //search for the letter in the original word
		  for (int x = 0; x < 5; x = x + 1)
		  {
			  if (origWord.substr(x, 1) == letter);
			  {
				   //replace the appropriate dash
				   //with the letter
				   guessWord.replace(x, 1, letter);
				   //indicate that a replacement was made
				   dashReplaced = 'T';
			  } //end if
		  } //end for
		  
		  //determine whether a replacement was made
		  if (dashReplaced = 'T')
		  {
			  //if the word does not contain any dashes, then
			  //the user guessed the word, so the game is over
			  if (static_cast<int>(guessWord.find("-", 0)) == -1)
			  {
				   gameOver = 'T';
				   cout << "Yes, the word is "
					   << guessWord << endl;
				   cout << "Great guessing!" << endl;
				   
			  }
			  else
			  {
				  //display the status of the guessed word
				  cout << "Guess this word: "
					  << guessWord << endl;
				  dashReplaced = 'F';
			  } //end if 
		  }
		  else //processed when no dash was replaced 
		  {
			   //update the counter variable
			   numIncorrect = numIncorrect +1;
			   //determine whether player 2 made
			   //10 incorrect guesses
			   if (numIncorrect == 10)
			   {
					//the game is over
					gameOver = 'T';
					cout << "Sorry, the word is "
						<< origWord << endl;
			   } //end if
		  } //end if
	} //end while
	system("PAUSE");
	return 0;
} //end of main function 


This post has been edited by Ragorism: 19 November 2009 - 10:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Incorrect output

#2 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Incorrect output

Posted 19 November 2009 - 10:36 PM

nvm.

This post has been edited by seeP+: 19 November 2009 - 10:37 PM

Was This Post Helpful? 0
  • +
  • -

#3 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Incorrect output

Posted 19 November 2009 - 10:57 PM

if (origWord.substr(x, 1) == letter);
			  {
				   //replace the appropriate dash
				   //with the letter
				   guessWord.replace(x, 1, letter);
				   //indicate that a replacement was made
				   dashReplaced = 'T';
				   break;
			  } //end if
		  } //end for
		  cout<<guessWord;
		  //determine whether a replacement was made
		  if (dashReplaced = 'T')
		  {
			  //if the word does not contain any dashes, then
			  //the user guessed the word, so the game is over
			  if (static_cast<int>(guessWord.find("-", 0)) == -1)
			  {
				   gameOver = 'T';
				   cout << "Yes, the word is "
					   << guessWord << endl;
				   cout << "Great guessing!" << endl;
				   
			  } 


Why do you ask the user to enter a letter, then set gameOver to T when subscript 0 has no dash, the user only enters 1 letter so the game is over?
Was This Post Helpful? 0
  • +
  • -

#4 Ragorism  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-September 09

Re: Incorrect output

Posted 19 November 2009 - 11:06 PM

View PostseeP+, on 19 Nov, 2009 - 09:57 PM, said:

Why do you ask the user to enter a letter, then set gameOver to T when subscript 0 has no dash, the user only enters 1 letter so the game is over?

Hmm... that could be the crux of the issue.
Was This Post Helpful? 0
  • +
  • -

#5 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 153
  • View blog
  • Posts: 1,394
  • Joined: 13-June 08

Re: Incorrect output

Posted 20 November 2009 - 12:30 AM

Just a suggestion, but all of these ='T' I see make me think you actually want a Boolean which is just a true/false switch.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1