Hi, I'm working on a hangman game as my final project for a c++ class. The program gets a random answer string from a list of words; here is the code at the beginning of the program:
int main ()
{
char guesses[26];
char wordToGuess [15];
string words[10];
string answer;
string reply;
int gameAnswer = 0;
int used = 0;
int WrongGuess = 0;
int error = 0;
char guess;
char letterToCheck;
bool found = false;
bool stopGame = false;
bool sameLetter = true;
bool winner = false;
Player PLAYER;
Player COMPUTER;
COMPUTER.setName("Computer");
while(!stopGame)
{
if(error == 3)
return(0);
winner = false;
cout << "\nDo you want to play hangman? " << endl << endl;
cin >> reply;
reply = strToUpper(reply);
gameAnswer = prompt_yn(reply);
if(gameAnswer == 0)
stopGame = true;
elseif (gameAnswer == -1)
{
cout << "\nPlease enter yes or no\n";
error++;
continue;
}
else
{
answer = getNextWord();
for(size_t i = 0; i <= answer.length(); i++)
answer[i] = toupper(answer[i]);
the program runs without errors but when it gets to this line of code:
answer = getNextWord();
I get a popup saying debug assertion failed... string subscript out of range. This is the first time the answer variable is used. The getNextWord function returns a random string from an array of words, Words[NUMWORDS]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
string getNextWord()
{
int Used = NUMWORDS;
int randomNum = 0;
string word;
time_t seed = time(NULL);
srand((unsignedint)seed);
if(Used > 0)
{
randomNum = rand() % Used;
word = Words[randomNum];
Words[randomNum] = Words[Used-1];
Used--;
return(word);
}
If i click ignore a couple times the error goes away and the program runs fine. This is the only issue I'm having with the program, and I want desperately to just be done with this project and this class! Any help would be greatly appreciated! Thanks!!