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;
}
}