string subscript out of range

Hi. I am having issues debugging this program. I get an error message saying Debug assertion failed and i cannot seem to figure out where the problem is or how to fix it. This is only part of the code but this where the debugging breaks. The expression says that the string subscript is out of range. If someone could help me figure this out it would be much appreciated. Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <ctime>
#include <cstdlib>


using namespace std; 
int main()
{
int wordOne[5];
ifstream animals;
animals.open("animalHangman.txt");
for (int i = 0; i < 5; i++)
{
 animals >> wordOne[i];
}
animals.close();

srand(time(0));
int randGen = 1 + rand() % 5;
string animalGuess;
animalGuess = wordOne[1];
cout << animalGuess;
cout << endl << animalGuess.length();
char* wordGuess = new char[animalGuess.length()];
for (unsigned int i = 0; animalGuess.length(); i++)
{
	int randPos = rand() % 2;
	if (randPos)
			{
				wordGuess[i] = animalGuess[i];
			}
			else
			{
				wordGuess[i] = '-';
			}
		}
		for (int i = 6; i >= 0; i--)
		{
			cout << wordGuess;

			char usersGuess=0;
			cout << endl << endl << "Enter the guessed word: ";
			cin >> usersGuess;
			for (unsigned int i = 0; i < animalGuess.length(); i++)
			{
				if (usersGuess == animalGuess[i])
				{
					wordGuess[i] = usersGuess;
				}
			};
			if (usersGuess == animalGuess[i])
			{
			cout << endl << "You guessed correctly.";
			}
			else if (i != 0)
			{
	cout << endl << "Try again." << endl << "You have " << i << " lives left,";
			}
			else
		cout << "You failed!" << endl << "Word was \'" << animalGuess << "\' .";
		}
	}
Last edited on
Check the condition on line 28. When does it become false and the loop end?
Topic archived. No new replies allowed.