Why crashes the console after running the program?

The program works correctly, correctly counts the number of words in a text file and removed correctly repeated word, but in the end, you receive the error
My code
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;


int SIZE;

struct WordCount
{
 string name;
 int count;
};

//sort words
void bubbleSort(string arr[], int size)
{
 for (int stopMarker = 1; stopMarker < size;++stopMarker)
{
 for (int j = size - 1; j >= stopMarker; j--)
{
 if (arr[j] < arr[j - 1])
{
 swap(arr[j] arr[j - 1]);
}
}
}
}

//count the number of words in a text file
int lenCount(int nCount){
 //int fSpace = 1, nCount = 0;
 int fSpace = 1;
 nCount = 0;
 char ch[2] = {0};
 FILE *fh = fopen("./words.txt", "r");
 while (! feof(fh))
{
 fgets(ch, 2, fh);
 if (ch[0] > 32)
 fSpace = 0;
 else if (0 == fSpace)
{
nCount++;
 fSpace = 1;
}
 ch[0] = 0;
}
fclose(fh);
 //printf("%d\n", nCount);
 //cout << nCount;
 return nCount;
}


int main()
{
 int nCount = 0;
 int x = lenCount(nCount);
 //cout << x << endl;
 SIZE = x;
 cout << SIZE << endl;
 string wordArray[SIZE];
 string temp;
 WordCount arr[SIZE];
 Item WordCount;


 ifstream input;

input.open("./words.txt");



 for (int i = 0; i < SIZE; i++)
{
 input >> wordArray[i];
 temp.erase(std::remove_if(temp.begin(), temp.end(), [](char c)
{
 return c == ',' || c == '.' || c == '!';
}),
temp.end());
}

input.close();

 bubbleSort(wordArray, SIZE);

 int j = 0;
 for (int i = 0; j < SIZE; i++)
{
 if (item.name != wordArray[j] && j < SIZE)
{
 item.name = wordArray[j];
 item.count = 1;
 arr[i] = item;
 j = j + 1;
}
 while (j < SIZE && item.name == wordArray[j])
{
item.count++;
 arr[i] = item;
 j = j + 1;
}
}



 for (int i = 0; i < 135; i++)
{
 //cout << setw(13) << left << arr[i].name << setw(10) << "Count =" << arr[i].count << endl;
 cout << setw(13) << left << arr[i].name << setw(10) << endl;
 //cout << arr[i].name;
}

 return 0;
}</algorithm></string></fstream></iomanip></iostream>


The text in the text file:

Labor idea face of Bush administration insistence training wage White House, an unrestricted six-month training wage time worker age job


Conclusion:
5b8ce55327b54263b0785812521335dd.png
June 26th 19 at 13:57
3 answers
June 26th 19 at 13:59
Solution
for (int i = 0; j < SIZE; i++)

at least, here the output i outside SIZE (check is on j instead of i).

the fall is due to overflow checks at the end of the program. something goes beyond the allocated memory..
if the change in the i same - carlos_Leannon commented on June 26th 19 at 14:02
how to fix, so as not to go beyond? - carlos_Leannon commented on June 26th 19 at 14:05
And why in the last cycle "i" < 135? Maybe then it is outside of the array? - Aric commented on June 26th 19 at 14:08
: maybe there is. all the code looks very strange. a few years ago, the compiler would generally not allowed to allocate memory in this way. spoiled programmers :) - turner.Beahan commented on June 26th 19 at 14:11
: although there is no read access will not lead to feil overflow checks. or falls not overflow. - turner.Beahan commented on June 26th 19 at 14:14
damn Riley for this was changed to 135 SIZE and has ceased to fly :D - carlos_Leannon commented on June 26th 19 at 14:17
June 26th 19 at 14:01
I skimmed through the code:
Here string wordArray[SIZE]; you need to use std::vector
Inside this loop can be outside of the array
int j = 0;
for (int i = 0; j < SIZE; i++)

or may not be, if you believe in the algorithm, then everything is OK
WTF?
for (int i = 0; i < 135; i++)
{
 //cout << setw(13) << left << arr[i].name << setw(10) << "Count =" << arr[i].count << endl;
 cout << setw(13) << left << arr[i].name << setw(10) << endl;
 //cout << arr[i].name;
 }

Where the magic number is 135?
And Yes, don't use fopen since I write in C++
what is bad to use fopen? - carlos_Leannon commented on June 26th 19 at 14:04
135 was the problem :D - carlos_Leannon commented on June 26th 19 at 14:07
:
what is bad to use fopen?

The fact that in C++ there is std::fstream he is better and more convenient. - Aric commented on June 26th 19 at 14:10
June 26th 19 at 14:03
Run in debug mode from the Studio and she'll show the place where error appears.
well, I already (probably) know where the error but can not fix - carlos_Leannon commented on June 26th 19 at 14:06

Find more questions by tags C++