.exe has stopped working windows is looking for a solution

I am trying to write a program that reads from a file, puts the words into an array counts how many times each word appears in the file, alphabetizes it and then prints the output to an output file. But when I run it is say .exe has stopped working windows is looking for a solution. I know it is opening the file, reading from it and putting into an array but I haven't been able to test if it has counted and alphabetized because of this error. Can you please see where I went wrong?

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

//global to this file

const std::string EXITCHAR = "X";
const int USER_CHOSE_TO_EXIT = -1;
const int SUCCESS = 0;
const std::string OUTPUTFILENAMEARRAY = "OutputArray.txt";
const int arraysize = 500;



struct entry {
string word;
int number_occurences;

}; // REMEMBER the last semi-colon
entry listOfWords[arraysize];
int lastIndexof = 0;
//return false if cannot open file
bool openFile(ifstream& myfile,const std::string& myFileName)
{
myfile.open(myFileName.c_str());
return true;
}

void closeFile(ifstream& myfile)
{
myfile.close();
}

//add a token to array or update its count
void processTokenArray(string &token) {
//listOfWords[lastIndexof].number_occurences;
//listOfWords[lastIndexof].word;
listOfWords[lastIndexof].number_occurences = 0;
int i = 0;
int j = 0;
for(;i < arraysize && listOfWords[i].word != token; i++)
{
if(listOfWords[i].word == token)
{
listOfWords[i].number_occurences++;
return;
}


else
{
while(listOfWords[j].number_occurences !=0)
{
j++;
}
}
}

listOfWords[lastIndexof].word + " " = token; // adds word to array
listOfWords[lastIndexof].number_occurences=1;
lastIndexof++;


cout << listOfWords[lastIndexof-1].word;

//TODO
}

void sortArray()
{

int x;
int y=0;
int flag = 1;
entry tempentry;
int lengtharray = arraysize;
for(x = 0; (x<=lengtharray); x++)
{

if((strcmp(listOfWords[lastIndexof].word.c_str(), listOfWords[lastIndexof+1].word.c_str())>0))
{

tempentry = listOfWords[y];
listOfWords[y]=listOfWords[y+1];
listOfWords[y+1] = tempentry;
}
if(lastIndexof>0)
{
lastIndexof = lastIndexof-2;
}
else
{
lastIndexof = lastIndexof -1;
}


//TODO
}
return;
}

//============================================================================
// utilities
//============================================================================
string NumberToString ( int Number )
{
ostringstream ss;
ss << Number;
return ss.str();
}

//take 1 line and extract all the tokens from it
//feed each token to processToken... for recording
void extractTokensFromLine(std::string &myString) {
const char CHAR_TO_SEARCH_FOR = ' ';
stringstream ss(myString);
string tempToken;

while (getline(ss, tempToken, CHAR_TO_SEARCH_FOR)) {
processTokenArray(tempToken);
}
}
//============================================================================
// output
//============================================================================
bool displayDataArray(const std::string &OUTPUTFILENAME)
{
ofstream Output(OUTPUTFILENAMEARRAY);
for(int m =0; m < arraysize; m++)
{
if(!(listOfWords[m].word == " " || listOfWords[lastIndexof].number_occurences == 0))
{
Output << listOfWords[m].word << ":" << listOfWords[m].number_occurences<< '\n';
}
//TODO
}
return true;
}

//============================================================================
// main please do not return magic numbers, look at the constants above for
// appropriate values
//============================================================================
int main() {
std::string myFileName = "TestData.txt";
ifstream myfile;
//string words[100];
while(true)
{
if (openFile(myfile,myFileName)==true)
{
std:: string line;
while(!myfile.eof())
{
getline(myfile,line);
extractTokensFromLine(line);
//cout<<line;
}
closeFile(myfile);
break;
}
else {
cout<<"Unable to open file. Please enter a new one or enter x to close the program"<< endl;
cin >> myFileName;


if(myFileName == EXITCHAR)
{
return USER_CHOSE_TO_EXIT;
}
}
//TODO

}
sortArray();
displayDataArray(OUTPUTFILENAMEARRAY);
return SUCCESS;

}
Topic archived. No new replies allowed.