Hangman game - string subscript out of range

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:

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

		else if (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((unsigned int)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!!
The only thing that looks fishy to me is this:
50
51
for(size_t i = 0; i <= answer.length(); i++) // This should be <, not <=
    answer[i] = toupper(answer[i]);
thank you, i ended up solving the problem - had to do with my getNextWord function trying to access the wrong character. It has been corrected :)
Topic archived. No new replies allowed.