_Xlength_error("string too long");??

Mar 18, 2020 at 10:00pm
Hi all

I have a program which will read in text files and search for specified words.

Im having an error which shows up as soon as i exit the for loop in my function which brings me to:

[[noreturn]] static void _Xlen() {
_Xlength_error("string too long");
}



Here is the function in question:


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
  void searchForFound(string foundWords[], string txt1Arr[], string word1[], string word2[], string word3[], string word4[], string word5[], string word6[])
{
	string word1txt = "dog";
	string word2txt = "pig";
	string word3txt = "cat";
	string word4txt = "rat";
	string word5txt = "fox";
	string word6txt = "cow";
	bool word1Found = false;
	bool word2Found = false;
	bool word3Found = false;
	bool word4Found = false;
	bool word5Found = false;
	bool word6Found = false;
	int foundWord1Index;
	int foundWord2Index;
	int foundWord3Index;
	int foundWord4Index;
	int foundWord5Index;
	int foundWord6Index;
	for (int i = 0; i < 14; i++)
	{
		string joined =  txt1Arr[i] + txt1Arr[i + 1] + txt1Arr[i + 2];
		for (int i = 0; i < 14; i++)
		{
			if (joined == word1txt)
			{
				word1Found = true;
				foundWord1Index = i;
			}
			if (joined == word2txt)
			{
				
				 word2Found = true;
				 foundWord2Index = i;
			}
			if (joined == word3txt)
			{
				
				 word3Found = true;
				 foundWord3Index = i;
			}
			if (joined == word4txt)
			{
				
				 word4Found = true;
				 foundWord4Index = i;
			}
			if (joined == word5txt)
			{
				
				 word5Found = true;
				 foundWord5Index = i;
			}
			if (joined == word6txt)
			{
				
				 word6Found = true;
				 foundWord6Index = i;
			}
		}


	 
	}


	if (word1Found == false)
	{
		cout << word1 << ", Not Found!" << "Location N/A" << endl;
	}

	if (word2Found == false)
	{
		cout << word2 << ", Not Found!" << "Location N/A" << endl;
	}
	if (word3Found == false)
	{
		cout << word3 << ", Not Found!" << "Location N/A" << endl;
	}

	if (word4Found == false)
	{
		cout << word4 << ", Not Found!" << "Location N/A" << endl;
	}

	if (word5Found == false)
	{
		cout << word5 << ", Not Found!" << "Location N/A" << endl;
	}

	if (word6Found == false)
	{
		cout << word6 << ", Not Found!" << "Location N/A" << endl;
	}

	if (word1Found == true)
	{
		cout << word1txt << ", Found!" << "Location " << foundWord1Index << endl;
	}
	if (word2Found == true)
	{
		cout << word2txt << ", Found!" << "Location " << foundWord2Index << endl;
	}
	if (word3Found == true)
	{
		cout << word3txt << ", Found!" << "Location " << foundWord3Index << endl;
	}
	if (word4Found == true)
	{
		cout << word4txt << ", Found!" << "Location " << foundWord4Index << endl;
	}
	if (word5Found == true)
	{
		cout << word5txt << ", Found!" << "Location " << foundWord5Index << endl;
	}
	if (word6Found == true)
	{
		cout << word6txt << ", Found!" << "Location " << foundWord6Index << endl;
	}
}




Here is the things i have included up top:
1
2
3
4
#include <iostream>
#include <fstream>
#include <string>
using namespace std;



And here is Main:
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
int main()
{
	//Reading in the two required files
	ifstream infiletxt1("C:\\Users\\jackf\\Desktop\\Programming Assignment 2(New)\\text1.txt");
	ifstream infilesearch1("C:\\Users\\jackf\\Desktop\\Programming Assignment 2(New)\\search1.txt");

	
	string text1Array[14];
	string search1Array[18];
	string foundWords[18];
	string word1[3] = {"d","o","g"};
	string word2[3] = { "p","i","g" };
	string word3[3] = { "c","a","t" };
	string word4[3] = { "r","a","t" };
	string word5[3] = { "f","o","x" };
	string word6[3] = { "c","o","w" };


	
	

	readFileText1(infiletxt1, text1Array);
	cout << "" << endl;
	readFileSearch1(infilesearch1, search1Array);
	outputArraySize14(text1Array);
	cout << "" << endl;
	outputArraySize18(search1Array);
	cout << "" << endl;
	system("pause");
	system("CLS");

	cout << "Begin Searching for words:" << endl;

	system("pause");
	system("CLS");

	compareArray(text1Array, search1Array, foundWords);
	searchForFound(foundWords, text1Array, word1, word2, word3, word4, word5, word6);

	system("pause");
	system("CLS");

	





	
}



The function correctly changes the bools of which words are in the array and works as intended but for some reason the error comes up when the program supposedly leaves the for loop





Mar 18, 2020 at 10:19pm
1
2
3
	for (int i = 0; i < 14; i++)
	{
		string joined =  txt1Arr[i] + txt1Arr[i + 1] + txt1Arr[i + 2];


No idea what you are doing, but, from the way it was called, txt1Arr[] has 14 elements, so both txt1Arr[i + 1] and txt1Arr[i + 2] go out of bounds when i = 13, and the latter also when i=12.

You do know that
if (word1Found == true)
could be simplified to
if (word1Found)
don't you? (Also, many other examples.) Your code is also highly repetitive.
Last edited on Mar 18, 2020 at 10:21pm
Mar 18, 2020 at 10:22pm
Your code can be massively simplified:

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
void searchForFound( string foundWords[], string txt[], string word[] )
{
    bool found[6] = { false };
    int index[6] = { 0 }

    for( int i = 0; i < 14; i++ )
    {
        string joined =  txt[i] + txt[i + 1] + txt[i + 2];
        for( int i = 0; i < 14; i++ )
            for( int j = 0; j < 6; ++j )
                if( joined == word[j] )
                {
                    found[j] = true;
                    index[j] = i;
                }
    }

    for( int i = 0; i < Size; ++i )
    {
        if( found[i] )
            cout << word[i] << ", Found!" << "Location " << index[i] << '\n';
        else
            cout << word[i] << ", Not Found!" << Location N/A\n";
    }
}


int main()
{
	string dir = "C:/Users/jackf/Desktop/Programming Assignment 2(New)/";
	ifstream infiletxt1( dir + "text1.txt" );
	ifstream infilesearch1( dir + "search1.txt" );
	
	string text1Array[14];
	string search1Array[18];
	string foundWords[18];

	readFileText1( infiletxt1, text1Array);
	cout << '\n';
	readFileSearch1( infilesearch1, search1Array );
	outputArraySize14( text1Array );
	cout << '\n';
	outputArraySize18( search1Array );
	cout << '\n';

	cout << "Begin Searching for words:\n";

	compareArray( text1Array, search1Array, foundWords );
	searchForFound( foundWords, text1Array, word );
} 

This is a very inefficient way to search for strings.
Last edited on Mar 18, 2020 at 10:24pm
Mar 18, 2020 at 11:00pm
Thank you, that solved all my problems with arrays going out of bounds and now the code works as intended.

Cheers for the help
Topic archived. No new replies allowed.