Hello, I am having trouble creating a double linked list within an array of pointers. Right now actually, I am just trying to make a linked list. Line 267 is where I get a complier error. I understand the error (cannot convert `std::string' to `wordList*' in assignment ), but I just don't know how to assign the first value to an empty list. I am also having trouble still initialzing the list. According to examples, line 160 should be: currentWord->head = NULL instead of current = NULL. But I can't compile when I use the right method. Any ideas on how I can fix this? Thanks in advance.
//*****************************************************************************
// CODE FILENAME: MathewsTerriAssn1.cpp
// DESCRIPTION: Program analyzes words in a text file.
// DATE:
// DESIGNER: Terri Mathews
// FUNCTIONS: 1) Main: Calls functions, closes files, closes program
// 2)
//*****************************************************************************
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <fstream>
#include <string>
using namespace std;
// Global constants:
const int SIZE_ALPHABET = 26; // number of letters in alphabet
struct word{
string newWord;
word *next;
word *back;
};
struct wordList{
word *head;
};
wordList *letter[SIZE_ALPHABET];
// Function Prototypes
bool Verify_Command_Line(int argc);
bool Open_Verify_Data_File(char *argv[], ifstream &inFile);
wordList* Initialize_Linked_Lists();
void Read_File(string &getWord, ifstream &inFile);
void Initialize_Array();
bool Discard_Word(string getWord);
void Build_List(string getWord);
void Correct_Word(string &getWord);
void To_Upper(string &getWord);
bool Find_In_List(wordList *letter[], string getWord);
void Add_To_List(string getWord, wordList *letter[]);
int main(int argc, char *argv[])
{
ifstream inFile; // variable to read data in from file
ofstream outFile; // variable to output data to file
bool endProg;
wordList *current;
string getWord;
bool discardWord;
endProg = Verify_Command_Line(argc);
if(!endProg)
{
endProg = Open_Verify_Data_File(argv, inFile);
if (!endProg)
{
Initialize_Array();
Read_File(getWord, inFile);// Eventually this function will be called
// within another function.
Build_List(getWord);
}
}
inFile.close(); // closes input file
cout << endl << endl;
system ("PAUSE");
return 0;
}
bool Verify_Command_Line(int argc)
{
bool endProg = true;
if (argc <= 1)
{
cout << endl << "The data file name was not given as an argument"
<< " on the command line." << endl << "Please give the name of"
<< " your data file as a command line argument, and " << endl
<< "run the program again." << endl << endl;
return endProg;
}
else
{
endProg = false;
return endProg;
}
} // End of function
bool Open_Verify_Data_File(char *argv[], ifstream &inFile)
{
bool endProg = true;
//ifstream inFile; // file input stream
string filename; // name of file given as command line argument
filename = argv[1];
inFile.open(filename.c_str());
if(!inFile)
{
cout << "I'm sorry, that filename does not exist. Please re-enter"
<< endl << "the file name as a command line argument and run the"
<< endl << "program again." << endl << endl;
return endProg;
}
else
{
endProg = false;
return endProg;
}
}
wordList* Initialize_Linked_Lists()
{
// function to create list
char ch;
// attempt to allocate list structure
wordList *currentWord = new (nothrow) wordList;
// check if allocation successful
if (currentWord == NULL) {
cout << "Error - out of heap space!!" << endl;
cout << "Press C to continue: ";
cin >> ch;
}
else
// initialize to empty list
currentWord = NULL;
return currentWord;
}
void Initialize_Array()
{
wordList *currentWord;
for (int count = 0; count < SIZE_ALPHABET; count++)
{
currentWord = Initialize_Linked_Lists();
letter[count] = currentWord;
cout << letter[count] << " "; //(TEST ONLY)
}
}
void Read_File(string &getWord, ifstream &inFile)
{
inFile >> getWord;
}
void Build_List(string getWord)
{
bool discardWord;
string correctWord;
discardWord = Discard_Word(getWord);
if(discardWord == false)
{
Correct_Word(getWord);
To_Upper(getWord);
Add_To_List(getWord, letter);
}
}
bool Discard_Word(string getWord)
{
int len;
bool discardWord = false;
int num;
len = getWord.length();
for (int count = 0; count < len; count++)
{
if (getWord[count] < '9' && getWord[count] > '0')
{
discardWord = true;
count = 100;
}
}
return discardWord;
}
void Correct_Word(string &getWord)
{
int len;
len = getWord.length();
for (int count = 0; count < len; count++)
{
if((getWord[count] >= '!' && getWord[count] <= '&') ||
(getWord[count] >= '(' && getWord[count] <= ',') ||
(getWord[count] >= '.' && getWord[count] <= '/'))
getWord.erase(count, 1);
}
}
void To_Upper(string &getWord)
{
std::transform(getWord.begin(), getWord.end(), getWord.begin(), ::toupper);
}
void Add_To_List(string getWord, wordList *letter[])
{
int firstLetter;
bool wordFound;
char firstChar = getWord[0];
int index = firstChar - 'A';
wordList *currentWord;
word *nextNewWord = new (nothrow) word;
currentWord = Initialize_Linked_Lists();
nextNewWord->newWord = getWord;
nextNewWord->next = NULL;
if(letter[index] == NULL)
{
cout << endl << "test1" << endl;
letter[index] = getWord; // ERROR MESSAGE: cannot convert `std::string' to `wordList*' in assignment
word *next = NULL;
word *back = NULL;
}
else
{
nextNewWord->next = currentWord->head;
currentWord->head = nextNewWord;
}
}