String subscript out of range. for loop conditions all look good

Im having a problem with my sneaky hangman game I feel like im really close just getting an error I can't fix. When I run it, it says something like string subscript out of range when I goggled it. it says im searching past the bounds of what it should be or something. Any idea what could be wrong?

Also a dictionary.txt file is needed to run the program

code: http://pastebin.com/JHLSGN6Y



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

int guessesUsed = 0;
int wordlength;
int full = 0;
int count = 0;
int maxFreq = 0;
int ltrCount;
char guess;
char letter;
bool success = false;
string word;
string dashes = string(wordlength, '_');
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string mostFrequent;
vector<string> list;
vector<string> templist;
map<string, int> occurance;




void getLength()
{
	tryagain:
	wordlength = 0;
	cout << "Please enter length of word you wish to guess \n";
	cin >> wordlength;
	dashes = string(wordlength, '_');
	ifstream infile("dictionary.txt");
	while (getline(infile, word))
	{
		if (word.length() == wordlength)
		{
			list.push_back(word);
		}
	}
	if (list.size() == 0)
	{
		cout << "There are no words of this length " << endl;
		goto tryagain;
	}
}

bool validGuess(char letter)
{

	//checks if the guess is in the alphbet
	size_t found = alphabet.find(letter);



	if (found != string::npos)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void removeLetter(char letter , string mostFrequent)
{
	//finds the letter to be removed

	size_t found = alphabet.find(letter);

	if (found != string::npos)
	{
		alphabet[found] = '0';
	}
	ltrCount = 0;
	for (int i =0; i < mostFrequent.length(); i++)
	{
		if (mostFrequent[i] != '_')
		{
			ltrCount++;
		}
	}
	string dance = string(wordlength, '_');
	for (int i = 0; i < list.size(); i++)
	{
		bool ltrmatch = true;
		int incr = 0;
		int scndCount = 0;
		string wrd = list[i];
		for (int j = 0; j < mostFrequent.length(); j++)
		{
			if (mostFrequent[i] == letter)
			{
				if (wrd[i] != letter)
				{
					ltrmatch = false;
				}
			}
		}
		if (ltrmatch == true)
		{
			templist.push_back(wrd);
		}			
	}
	
	list.clear();
	list = templist;
	for (int i = 0; i < list.size(); i++)
	{
		cout << list[i] << endl;
	}
	templist.clear();
	
}

void giveLetter(string mostFrequent, char letter)
{
	
	int full = 0;
	for (int i = 0; i < mostFrequent.length(); i++)
	{
		if (mostFrequent[i] != '_')
		{
			dashes[i] = mostFrequent[i];
		}
		if (dashes[i] != '_')
		{
			full++;
		}
	}
	if (full != mostFrequent.length())
	{
		guessesUsed++;
		cout << endl;
		cout << "\nGuesses used is " << guessesUsed << endl;
	}
	if (full == word.length() || full == word.length() - 1)
	{
		success = true;
	}
	removeLetter(letter, mostFrequent);
	cout << endl;
	cout << "word so far is : " << dashes << endl;
	cout << endl;
}

void VecFreqCheck(char letter)
{
	string temp;
	for (int m = 0; m < list.size(); m++)
	{
		string underscore = string(wordlength, '_');
		temp = list[m];
		for (int p = 0; p < temp.length(); p++)
		{
			//replaces the underscore with the correctly guessed letter
			if (letter == temp[p])
			{
				underscore[p] = letter;
			}
		}
		//adds the unscored word with the guess filled in to occurance or increments its value if its aready added
		occurance[underscore]++;
	}

	for (map<string, int>::iterator it = occurance.begin(); it != occurance.end(); ++it)
	{
		cout << it->first;
		cout << ":  ";
		cout << it->second << endl;
		if (it->second > maxFreq)
		{
			mostFrequent = it->first;
			maxFreq = it->second;
		}
	}
	cout << endl;
	cout << mostFrequent << endl;
	giveLetter(mostFrequent, letter);
	occurance.clear();
}


void guessA()
{
	getLength();
	do
	{

		//Takes in guess
		cout << "Enter guess "<<endl;
		cin >> guess;

		//If the the guess is incorrect it will increment check 
		if (validGuess(guess) == true)
		{
			VecFreqCheck(guess);

		}
		else
		{
			cout << "invalid guess, try again" << endl;
		}

	} while (guessesUsed < wordlength + 9 && success == false);
	if (success == true)
	{
		cout << "YOU WIN" << endl;
	}
	else
		cout << "YOU LOSE";

}

int main()
{
	guessA();
}
Last edited on
Please use code tags:
http://www.cplusplus.com/articles/jEywvCM9/

You marked this topic as solved, did you solve your problem already?
Added the code tags thanks! No clicked that by accident still no result :(
Fixed it on line 97 it should be mostFrequent[j]
Topic archived. No new replies allowed.