So this is my first time on here asking for help but I do visit this forum every now and again when I am taking a programming class. I am trying to get my doubly linked list to work correctly. Also I am having issues with parsing an input file. The function for the parsing is checkInputWord, and I wanted to see if you guys could see what I was messing up. Then in the insertWord function I am trying to either add a node to an empty array of pointers or one that already has some pointers. Here is my code and hopefully it is written well enough so you can understand what I am trying to do.
# include <iostream>
# include <iomanip>
# include <fstream>
# include <string>
# include <cctype>
# include <cstddef>
using namespace std;
const string WORD_FILE = "listing.txt";
const int MAX_SIZE = 26;
struct wordList {
string newWord;
wordList *previous;
wordList *next;
};
void programIntro();
void checkTextFile (ifstream& wordIn);
void initializeArray (wordList *wordArray[], int &index);
void readTextFile (ifstream& wordIn, wordList* wordArray [], int &index, int arrayCount, string &word1);
void checkInputWord (ifstream &wordIn, wordList* wordArray [], string &word1);
void insertWord (wordList *wordArray [], int &index, string &word1);
int main () {
wordList* theWord[MAX_SIZE];
int arrayCount = 0;
int indexes = 0;
int count;
string words;
ifstream wordIn;
programIntro();
checkTextFile (wordIn);
initializeArray(theWord, indexes);
readTextFile (wordIn, theWord, indexes, arrayCount, words);
system ("pause");
return 0;
}
void programIntro ()
{
cout << "**************************************************************************" <<endl;
cout << "This program will search a text file and place the words in the text file" << endl;
cout << "into a doubly linked list." << endl << endl;
cout << "**************************************************************************" << endl;
return;
}
void checkTextFile (ifstream& wordIn)
{
wordIn.open (WORD_FILE.c_str());
if (!wordIn)
cout << "Error! Text File did not open!" << endl;
else
cout << "File opened correctly!" << endl;
return;
}
void initializeArray (wordList *wordArray[], int &index)
{
for (index = 0; index < MAX_SIZE; index++) {
wordArray[index] = NULL;
}
return;
}
void readTextFile (ifstream& wordIn, wordList* wordArray [], int &index, int arrayCount, string &word1)
{
char character;
char letter;
arrayCount = 0;
string word;
wordIn >> word1;
cout << " READ WORD: " << word1 << endl;
cout << " ARRAY count = " << arrayCount << endl;
character = word1[0];
letter = toupper(character);
index = letter - 'A';
cout << " Index for Word: " << index << endl << endl;
checkInputWord (wordIn, wordArray, word1);
insertWord (wordArray, index, word1);
while (wordIn.eof() != true) {
arrayCount++;
wordIn >> word1;
checkInputWord (wordIn, wordArray, word1);
character = word1[0];
letter = toupper(character);
index = letter - 'A';
cout << " READ WORD: " << word1 << endl;
cout << " ARRAY count = " << arrayCount << endl;
cout << " Index for Word: " << index << endl << endl;
insertWord (wordArray, index, word1);
}
return;
}
void checkInputWord (ifstream &wordIn, wordList *wordArray [], string &word1)
{
int curr_position = 0;
int next_position;
string badWord;
next_position = word1.find_first_of(",.;:)(*&^%$#@!1234567890", curr_position);
badWord = word1.substr(curr_position, next_position - curr_position);
cout << " Current position: " << curr_position << endl << endl;
cout << " Next position: " << next_position << endl ;
cout << " Bad Word: " << badWord << endl ;
if (next_position != string::npos) {
curr_position = next_position + 1;
next_position = word1.find_first_of(" ,.;:)(*&^%$#@!1234567890", curr_position);
badWord = word1.substr(curr_position, next_position - curr_position);
cout << " Current position: " << curr_position << endl << endl;
cout << " Next position: " << next_position << endl << endl;
cout << " Bad Word: " << badWord << endl << endl;
}
return;
}
void insertWord (wordList *wordArray [], int &index, string &word1)
{
wordList *theList;
if (wordArray[index] == NULL) {
theList = new wordList;
theList->newWord = word1;
theList->next = NULL;
theList->previous = NULL;
wordArray[index] = theList;
cout << wordArray[index]->newWord << endl;
}
else {
wordList *newNode = new wordList;
newNode->newWord = word1;
newNode->previous = NULL;
newNode->next = wordArray[index];
wordArray[index]->previous = newNode;
wordArray[index]->next = wordArray[index]->previous;
cout << wordArray[index]->previous->newWord << endl;
cout << wordArray[index]->next->newWord<< endl;
}
return;
}