I am writing this program that is supposed to read a file, put the data into an array, alphabetize, count how many times each word appears and print to an output function. I posted before on how my program had an error when I ran it but I have fixed that. Now my outputArray file is empty. It gets created but there's nothing in it. I have been starting at this for hours and my mental capacity is wearing down. Any help is appreciated
// Project2_Starter.cpp : Defines the entry point for the console application.
//
//============================================================================
// Name : Project2.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description :
// see
http://www.cplusplus.com/doc/tutorial/files/
//============================================================================
#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()); //look over this
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 = 1; (x<lengtharray); x++)
{
if((strcmp(listOfWords[x-1].word.c_str(), listOfWords[x].word.c_str())>0))
{
tempentry = listOfWords[x];
listOfWords[x]=listOfWords[x+1];
listOfWords[x+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;
}