trying to loop my entire program (while true)

Hey everyone,

I'm trying to loop my entire program by encapsulating it in while true braces.

At the end of int main(), I reset all variables and vectors to 0.

The error I'm getting is "terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr"

I'm gathering from this that one of my statements that uses substr.() is stepping out of range, but I reset everything to zero so I'm not really sure why its stepping out of bounds. Any suggestions?

Thanks cpp community!

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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> initVect(vector<string> v) {
	for(unsigned int i = 0; i < v.size(); ++i) {
		v.erase(v.begin(),v.end());
	}
	return v;
}

vector<size_t> reinitVect(vector<size_t> v) {
	for(unsigned int i = 0; i < v.size(); ++i) {
		v[i] = 0;
	}
	return v;
}


int main(){
	while(true){

	string s;
	vector<string> word;
	cout << "Enter a string: ";
	getline(cin, s);
	size_t found;
	size_t found1;
	found = s.find_first_not_of(" ");
	found1 = s.find_first_of(" ");
	while(found1 != string::npos){
		word.push_back(s.substr(found,found1-found));
		found = found1 + 1;
		found1 = s.find_first_of(" ", found1 + 1);
	}
	found1 = string::npos;
	int count = 0;
	word.push_back(s.substr(found, found1));
	for (unsigned int i = 0; i < word.size(); ++i ){
		if (word[i].substr(word[i].length() - 1) == ".")
			word[i] = word[i].substr(0, word[i].length() - 1);
		if (word[i].substr(word[i].length() - 1) == ",")
			word[i] = word[i].substr(0, word[i].length() - 1);
		if(word[i].length() > 3){
			if (word[i].substr(word[i].length() - 2, 1) == "'")
				word[i] = word[i].substr(0, word[i].length() - 2);
		}
	}
	string sWord;
	cout << "What word would you like to search for?: ";
	cin >> sWord;
	unsigned int max = 0;
	for (unsigned int i = 0; i < word.size(); ++i){
		if(word[i] == sWord)
			++count;
	}
	unsigned int i = 0;
	vector<size_t> equals;
	if (word[i].length() == word[i+1].length()){
		equals.push_back(i);
		equals.push_back(i+1);
		max = i;
	}

	if (word[i].length() > word[i+1].length())
		max = i;
	if (word[i].length() < word[i+1].length())
			max = i+1;
	for (unsigned int i = 2; i < word.size() ; ++i) {
		if (word[max].length() == word[i].length()){
				equals.push_back(max);
				equals.push_back(i);
		}
		if ( word[max].length() < word[i].length() )
		    max = i;
	}
	vector<string> maxW;
	maxW.push_back(word[max]);

	if (equals.size() != 0){
		if (word[max].length() == word[equals[equals.size() - 1]].length()){
			if (equals.size() != 1) {
				for (i = 0; i < equals.size(); ++i){
					if (word[max].length() == word[equals[i]].length()){
						maxW.push_back(word[equals[i]]); //vector of largest words, all of the same length.
					}
				}
			}
		}
	}
	if (maxW.size() != 1) {
		for (unsigned int i = 0; i < maxW.size(); ++i){
			unsigned int d = 0;
			while (d < maxW.size()){
				if (i != d) {
					if(maxW[i] == maxW[d])
						maxW.erase (maxW.begin()+d);
				} ++d;
			}
		}
	}
	if ( maxW.size() != 1){
		cout << "\nThe strings with the most characters are ";
		for (unsigned int i = 0; i < maxW.size(); ++i ){
			cout << maxW[i];
			if (i != maxW.size() - 1)
				cout << " & ";
			else {
				cout << ". ";
				break;
			}
		}
	}
	else {
		cout << "\nThe string with the most characters is " << maxW[maxW.size() - 1] << ".";
	}
	if (count == 1)
		cout << "\nThere is " << count << " instance of the word " << sWord << "." << endl;
	else
		cout << "\nThere are " << count << " instances of the word " << sWord << "." << endl;


	word = initVect(word);
	maxW = initVect(maxW);
	equals = reinitVect(equals);
	found = 0;
	found1 = 0;
	max = 0;
	i = 0;
	count = 0;

	}
	return 0;
}


Works perfectly for me, but my compiler, after executing and looping it, says "This application has requested the Runtime to terminate in an unusual way.", then returns "3".
I would recommend declaring everything before starting the while loop.

Also, your program says "the string with the most characters is ", what does that mean? And if I put only 1 word and search that exactly word, the program crashes.
Last edited on
thanks samrux, I'll fix the bugs and repost.

It should be outputting the string with the most chars. I haven't tried entering one word until now, and yeah, it crashes. gotta fix it :D
Well, I solved the problem of the program crashing on one word. Thanks for letting me know!

The program still terminates if I try to loop it continuously.

I thought it was generally considered bad form nowadays to declare everything at the head of the program. I read its best to declare variables closest to where you use them. I'm a newbie so I'm not sure, just what I read. If everything resets to 0 at the end, why would it step out of bounds?

the end of the code is just me resetting everything I can think of, but it still crashes T^T

Any suggestions?

Thanks again!

Updated code:
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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> initVect(vector<string> v) {
	for(unsigned int i = 0; i < v.size(); ++i) {
		v.erase(v.begin(),v.end());
	}
	return v;
}

vector<size_t> reinitVect(vector<size_t> v) {
	for(unsigned int i = 0; i < v.size(); ++i) {
		v[i] = 0;
	}
	return v;
}

vector<string> ddVect(vector<string> v){
	for (unsigned int i = 0; i < v.size(); ++i){
		unsigned int d = 0;
		while (d < v.size()){
			if (i != d) {
				while (v[i] == v[d] && d < v.size()){
					v.erase (v.begin()+d);
				}
			} ++d;
		}
	}
	return v;
}

vector<string> delNonAlphaV(vector<string> v) {
	for (unsigned int i = 0; i < v.size(); ++i ){
		if (v[i].substr(v[i].length() - 1) == ".")
			v[i] = v[i].substr(0, v[i].length() - 1);
		if (v[i].substr(v[i].length() - 1) == ",")
			v[i] = v[i].substr(0, v[i].length() - 1);
		if(v[i].length() > 3){
			if (v[i].substr(v[i].length() - 2, 1) == "'")
				v[i] = v[i].substr(0, v[i].length() - 2);
		}
	}
	return v;
}


int main(){
	while(true){

	string s;
	vector<string> word;
	cout << "Enter a sentence: ";
	getline(cin, s);
	size_t found;
	size_t found1;
	found = s.find_first_not_of(" ");
	found1 = s.find_first_of(" ");
	size_t *pnt = &found1;
    unsigned long const L = 18446744073709551615;
	if ( *pnt == L)
		word.push_back(s);
	else{
		while(found1 != string::npos){
			word.push_back(s.substr(found,found1-found));
			found = found1 + 1;
			found1 = s.find_first_of(" ", found1 + 1);
		}
		found1 = string::npos;
		word.push_back(s.substr(found, found1));
	}
	word = delNonAlphaV(word);
	string sWord;
	cout << "What word would you like to search for?: ";
	cin >> sWord;
	int count = 0;
	for (unsigned int i = 0; i < word.size(); ++i){
		if(word[i] == sWord)
			++count;
	}
	unsigned int i = 0;
	unsigned int max = 0;
	vector<size_t> equals;
	if (word.size() > 1) {
		if (word[i].length() == word[i+1].length()){
			equals.push_back(i);
			equals.push_back(i+1);
			max = i;
		}
		if (word[i].length() > word[i+1].length())
			max = i;
		if (word[i].length() < word[i+1].length())
			max = i+1;
		for (unsigned int i = 2; i < word.size() ; ++i) {
			if (word[max].length() == word[i].length()){
				equals.push_back(max);
				equals.push_back(i);
			}
			if ( word[max].length() < word[i].length() )
				max = i;
		}
	}
	vector<string> maxW;
	maxW.push_back(word[max]);
	if (word.size() > 1) {
		if (equals.size() != 0){
			if (word[max].length() == word[equals[equals.size() - 1]].length()){
				if (equals.size() != 1) {
					for (i = 0; i < equals.size(); ++i){
						if (word[max].length() == word[equals[i]].length()){
							maxW.push_back(word[equals[i]]); //vector of largest words, all of the same length.
						}
					}
				}
			}
		}
	}
	if (maxW.size() != 1) {
		maxW = ddVect(maxW);
	}
	if ( maxW.size() != 1){
		cout << "\nThe strings with the most characters are ";
		for (unsigned int i = 0; i < maxW.size(); ++i ){
			cout << maxW[i];
			if (i != maxW.size() - 1)
				cout << " & ";
			else {
				cout << ". ";
				break;
			}
		}
	}
	else {
		cout << "\nThe string with the most characters is " << maxW[maxW.size() - 1] << ".";
	}
	if (count == 1)
		cout << "\nThere is " << count << " instance of the word " << sWord << "." << endl;
	else
		cout << "\nThere are " << count << " instances of the word " << sWord << "." << endl;


	word = initVect(word);
	maxW = initVect(maxW);
	equals = reinitVect(equals);
	found = 0;
	found1 = 0;
	max = 0;
	i = 0;
	count = 0;
	cin.clear();
	cin.sync();

	}
	return 0;
}
There isn't any problems on declaring the variabels before the loop, declare everything there without values, assign the default values at the starting of the while loop and remove the value reset at the end of it. See if that works, maybe someone else will help you more.

PD: Does unsigned long const L = 18446744073709551615; work for you? My compiler gives me an error, maybe you did something wrong before posting it or my compiler is messed up...
Last edited on
yeah it works for me, I hope it doesn't work for me and only me lol. I posted another thread about that fix because it seemed unconventional, but it worked. Guess what I'm learning is it's only a local fix >.<

I'll figure something out, always learning!

I'll try what you suggest, thanks!
Topic archived. No new replies allowed.