Subscript out of range error

Mar 19, 2018 at 2:37am
I dont understand why i get the "Debug assertion fail" error and it says vector subscript out of range.

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
  #include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<cctype>
using namespace std;

bool letter_checker(string w, char c) {

	int d=-1;


		d = w.find(c);
		if (d >= 0) {
			return true;
		}
		else {
			return false;
		
	}
}

bool apostrophe_checker(string v) {
	int apost_counter = 0;
	for (int i = 0; i < v.length(); i++) {
		if (v[i] == '\'') {
			apost_counter = apost_counter + 1;
		}


	}
	if (apost_counter > 0) {
		return true;
	}
}

int main() {
	string oneWord;
	//int counter = 0;
	vector<string> word_list;
	vector<char> guess_list;
	vector<string> new_word_list;
	int trials = 0;
	char letter_guess;
	vector<char> letter_guess_list;
	int length_choice, choice_word_counter = 0, x = 0;
	int total_word_count = 0;

	cout << "What length word do you want?" << endl;

	ifstream infile;
	infile.open("C:/Users/Nate Zegue/documents/dictionary.txt");

	if (!(infile.is_open())) {
		cout << "Unable to open file ditionary.txt" << endl;
	}
	else {
		cout << "" << endl;

	}
	cin >> length_choice;

	while (infile >> oneWord) {
		if (!(apostrophe_checker(oneWord) == true)) {
			total_word_count = total_word_count + 1;

		}
		if (oneWord.length() == length_choice) {
			for (int let = 0; let < oneWord.length(); let++) {
				oneWord[let] = toupper(oneWord[let]);
			}
			word_list.push_back(oneWord);
			choice_word_counter = choice_word_counter + 1;
		}

	}

	cout << "Total Number of words: " << total_word_count << endl;

	cout << "There are " << choice_word_counter << " words" << endl;
	while (trials <= 16) {
		cout << new_word_list.size() << endl;							//loops number of tries
		cout << "Guess a letter: " << endl;
		cout << "Letters Found: ";
		for (int i = 0; i < length_choice; i++) {

			cout << " _ ";

		}
		cin >> letter_guess;
		letter_guess = toupper(letter_guess);
		letter_guess_list.push_back(letter_guess);

		guess_list.push_back(letter_guess);

		for (int v = 0; v < word_list.size(); v++) {
			for (int j = 0; j < guess_list.size(); j++) {

				if (letter_checker(word_list[v], guess_list[j]) == false) {
					word_list.erase(word_list.begin() + v);
					//new_word_list.push_back(word_list[v]);
				}
			}

		}
		

		//cout << "Size of new list is: " << new_word_list.size() << endl;
		cout << "Size of new list is: " << word_list.size() << endl;
		cout << "Letters used so far: ";
		for (int b = 0; b < guess_list.size(); b++) {
			cout << guess_list[b] << " ";

		}
		cout << endl;
		trials = trials + 1;

	}



	//system("PAUSE");
	return 0;
}
Mar 19, 2018 at 2:45am
it says line 1795 but it doesn't even go that far.
Mar 19, 2018 at 2:59am
it runs here on the forums C++ shell until it tries to open the text file, i cant access the text file. hopefully someone smarter than me will chime in. whats in the text file? let me copy it into mine.
Last edited on Mar 19, 2018 at 2:59am
Mar 19, 2018 at 3:06am
Its a text file with like 40,000 words
Mar 19, 2018 at 3:07am
haha i figured. if someone doesnt answer this one tonight, i'll have you send me the text file and i'll try to play with it. we're going over the same stuff this week in class so it'll be good practice for me
Last edited on Mar 19, 2018 at 3:10am
Mar 19, 2018 at 3:20am
Im pretty sure its because of lines 96-105. but alright, appreciate the help.
Mar 19, 2018 at 11:11am
I think you're right. If the last element is removed word_list[v] will be out of bounds in the next iteration of the inner loop.
Topic archived. No new replies allowed.