Hi all,
I know you guys get a lot of questions regarding hangman game programs and I'm sorry to bog down the boards with another question regarding the game. I've combed through previous posts trying to see if my issue has been resolved and I have been unsuccessful. Here is my code for the game, I need help with the replace function in it. When I compile and run the program, it allows me to type in a word and shows me the blanks but when I try to guess a letter the program crashes.

The error I get is
Debug Assertion Failed!

//program path

Expression: String Subscript out of range

Here is my code:

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

using namespace std;
bool gameOver = false;
string origWord = " ";
string guess = " ";
int incorrect = 0;
int length = 0;
int chances = 6;
string letter;
string dashes = " ";
bool dashReplaced = false;
int main()
{
    cout<<"Enter a word: "<<endl;
    getline(cin, origWord);
	length = origWord.length();
dashes.assign(length, '-');
cout<<dashes<<endl;
while(gameOver == false)
{
     cout<<"Enter a letter: ";
     cin>>letter;
	 for(int i = 0; i <= length; i++)
	 {
      if(origWord[i] = letter[i])
	{
      replace(dashes.begin(), dashes.end(), '-', letter[i]);
      dashReplaced = true;
    }
    }
}
		if (dashReplaced != true)
		{
			cout<< "The letter " << letter << " is not in the word!" << endl;
		for (int x=chances; x<=6; x--)
			cout << "Misses left " << x << endl;

		}
	      if(dashReplaced == true)
		  {
            cout<<"Correct!"<<endl;
            cout<<"Guess this word: "<<endl;
            cout<<dashes<<endl;
            if(dashes.find("-", 0) == -1)
			{
                  cout<<"Great job! You win!"<<endl;
                  gameOver = true;
           }
		  }
           else
		   {
                  dashReplaced = false;
		   }
}

CODE Tags on the first post!!!! How did you happen to figure that out? Great!!!! :icon_mrgreen:

Please FORMAT your code properly. Change your IDE to replace TABs with 4 SPACEs. It is very difficult to read your code the way it's posted.

Don't you enter a single letter when asked? That would put the letter entered into letter[0] , not letter[i] .

I think the problem might be on line 26 , array out of bounds ? Shouldn't it be < then

@crutoy, I changed it but I still get the error.
The error gives the stdthrow.cpp (don't know what that means) but here it is if any of you can decipher it:

// throw -- terminate on thrown exception REPLACEABLE
#define _HAS_EXCEPTIONS 0
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <crtdbg.h>

_STD_BEGIN

#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
    {	// report error and die
    if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
    {
    ::_CrtDbgBreak();
    }
    }
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const unsigned short *message, const unsigned short *file, unsigned int line)
    {	// report error and die
    _Debug_message((wchar_t *) message, (wchar_t *) file, line);
    }

#endif

_STD_END

However, when I changed the letter to letter[0] the error no longer occurs (hooray!) but the program does not recognize when a correct letter is entered (or an incorrect one as it does not remove a chance from the chance counter).
Here is the piece of updated code with letter[0].

while(gameOver == false)
{
    cout<<"Enter a letter: ";
    cin>>letter;
      for(int i = 0; i < length; i++)
     {
      if(origWord[i] = letter[0])
     {
      replace(dashes.begin(), dashes.end(), '-', letter[0]);
      dashReplaced = true;
      }
      }

Formatting!!!!!! Please!!!!!

for(int i = 0; i < length; i++)
  {
     if(origWord[i] = letter[0])  // Is = the proper comparison operator?  Look carefully
     {
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.