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;
        }


}

Recommended Answers

All 3 Replies

Change line 160 back to currentWord->head = NULL; You know that is correct, so if something breaks then there is an error elsewhere. (It compiles fine for me with the correct assignment.)

For line 267, surely you don't mean to say that a std::string and a wordList* are the same kind of thing?
You'd have to say something like letter[ index ]->head->newWord = getWord; because (word*)->newWord is a std::string.

Since I've a headache and I'm tired I haven't bothered to examine your code too closely, and since it is not too obvious what it does (nor is it documented what it does) I can't say more than "are you sure what you are doing with an array of linked lists?"

Hope this helps.

Thank you, it does help. I will try the changes tonight after work. As for the array of linked lists....yeah, that is what we are required to do.

Hope your headache feels better soon!

Thanks again,
Terri

Hello Duoas....so I took your advice and played around some with my code, and it seems to be working better now (at least the words are added to the top of the linked list). The program is suppose to open a text file with English words, ignore words with numbers, strip words of punctuation, and capitalize all words. Then it should store the words in an array of pointers (one pointer per letter of alphabet). Then we are suppose to traverse the array forward to count the words in each double linked list, and then traverse it backwards to display the words. I am working on getting it to store the words now. Then I will move on to traversing the list. And I BARELY know what I am doing here with a double linked list array of pointers. :-) I am new to programming....and also will never be a programmer professionally. But it is something I must learn and understand, no matter how long it takes!! Thanks again for your help. Here is my corrected code. How's the headache?

//*****************************************************************************
//  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;
    string newWord;
};
   
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 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[], wordList *currentWord);


//*****************************************************************************
//  FUNCTION: Main
//  DESCRIPTION: Calls other functions and closes input and output files
//  INPUT: 
//      Parameters: N/A
//      Keyboard: N/A
//      File: N/A
//  OUTPUT: 
//      Return Val: N/A
//      Parameters: N/A
//      Display: N/A
//      File: N/A
//  CALLS TO: Read_File, Sort_Arrays, Output_To_File, User_Choice,
//            Search_For_Id, Final_Output
//*****************************************************************************
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 *currentWord;
    string getWord;
    bool discardWord;
    string correctWord;


    endProg = Verify_Command_Line(argc);
    
    if(!endProg)
    {
    
      endProg = Open_Verify_Data_File(argv, inFile);
    
      if (!endProg)
      {
                 
        Initialize_Array();
        
        while (!inFile.eof())
        {
             Read_File(getWord, inFile);
             discardWord = Discard_Word(getWord);
     
                   if(discardWord == false)
                   {
                       Correct_Word(getWord);
                       To_Upper(getWord);
                       Add_To_List(getWord, letter, currentWord);

                   }
        }
    }
}

    inFile.close();     // closes input file

    cout << endl << endl;
    
    system ("PAUSE");
    
    return 0;

}

//*****************************************************************************
//  FUNCTION: Verify_Command_Line
//  DESCRIPTION: Verifies that a command was given on the command line.
//  INPUT: 
//      Parameters: argc
//      Keyboard: N/A
//      File: N/A
//  OUTPUT: 
//      Return Val: 1, if command line argument not given
//      Parameters: N/A
//      Display: Error message if filename not listed as a command line
//               argument.
//      File: N/A
//  CALLS TO: N/A
//*****************************************************************************
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

//*****************************************************************************
//  FUNCTION: Open_Verify_Data_File
//  DESCRIPTION: Verifies that the given file name exists. If so, then opens
//               file.
//  INPUT: 
//      Parameters: argv
//      Keyboard: N/A
//      File: N/A
//  OUTPUT: 
//      Return Val: 2, if data file does not exist
//      Parameters: N/A
//      Display: Error message if data file does not exist.
//      File: N/A
//  CALLS TO: N/A
//*****************************************************************************

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;
    }
    
}

//*****************************************************************************
//  FUNCTION: Initialize_Linked_Lists
//  DESCRIPTION: Initializes double linked list.
//  INPUT: 
//      Parameters: argv
//      Keyboard: N/A
//      File: N/A
//  OUTPUT: 
//      Return Val: 2, if data file does not exist
//      Parameters: N/A
//      Display: Error message if data file does not exist.
//      File: N/A
//  CALLS TO: N/A
//*****************************************************************************

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->head = NULL;
 
  return currentWord;

 }

//*****************************************************************************
//  FUNCTION: Initialize_Array
//  DESCRIPTION: Initializes the array of pointers.
//  INPUT: 
//      Parameters: N/A
//      Keyboard: N/A
//      File: N/A
//  OUTPUT: 
//      Return Val: N/A
//      Parameters: N/A
//      Display: N/A
//      File: N/A
//  CALLS TO: Initialize_Linked_Lists
//***************************************************************************** 
void Initialize_Array()
{
     wordList *currentWord;
     
     for (int count = 0; count < SIZE_ALPHABET; count++)
     {    
         currentWord = Initialize_Linked_Lists();
         letter[count] = currentWord;
         
     }   
}

//*****************************************************************************
//  FUNCTION: Read_File
//  DESCRIPTION: Gets word from data file and passes back to main.          
//  INPUT: 
//      Parameters: N/A
//      Keyboard: N/A
//      File: N/A
//  OUTPUT: 
//      Return Val: N/A
//      Parameters: getWord, inFile
//      Display: N/A
//      File: N/A
//  CALLS TO: N/A
//*****************************************************************************
void Read_File(string &getWord, ifstream &inFile)
{
     inFile >> getWord;

}

//*****************************************************************************
//  FUNCTION: Discard_Word
//  DESCRIPTION: Determines whether a word should be discarded or not. Words are
//               discarded when a number is present.       
//  INPUT: 
//      Parameters: N/A
//      Keyboard: N/A
//      File: N/A
//  OUTPUT: 
//      Return Val: discardWord (true or false)
//      Parameters: getWord
//      Display: N/A
//      File: N/A
//  CALLS TO: N/A
//*****************************************************************************  
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;
}
     
//*****************************************************************************
//  FUNCTION: Correct_Word
//  DESCRIPTION: Removes non-letter characters from words except for ' and -.         
//  INPUT: 
//      Parameters: getWord
//      Keyboard: N/A
//      File: N/A
//  OUTPUT: 
//      Return Val: N/A
//      Parameters: getWord
//      Display: N/A
//      File: N/A
//  CALLS TO: N/A
//*****************************************************************************
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);
     } 
     
} 
   
//*****************************************************************************
//  FUNCTION: To_Upper
//  DESCRIPTION: Capitalizes all letters.          
//  INPUT: 
//      Parameters: getWord
//      Keyboard: N/A
//      File: N/A
//  OUTPUT: 
//      Return Val: N/A
//      Parameters: getWord
//      Display: N/A
//      File: N/A
//  CALLS TO: N/A
//*****************************************************************************
void To_Upper(string &getWord)
{
    std::transform(getWord.begin(), getWord.end(), getWord.begin(), ::toupper);    
}

//*****************************************************************************
//  FUNCTION: Add_To_List
//  DESCRIPTION: Adds word to array of pointers. Determines which pointer to 
//               use and also whether word is added to an empty list or to the
//               top of the double linked list.        
//  INPUT: 
//      Parameters: getWord, letter[], currentWord
//      Keyboard: N/A
//      File: N/A
//  OUTPUT: 
//      Return Val: N/A
//      Parameters: letter[]
//      Display: N/A
//      File: N/A
//  CALLS TO: N/A
//*****************************************************************************    
void Add_To_List(string getWord, wordList *letter[], wordList *currentWord)
{
     int firstLetter;
     bool wordFound;
     char firstChar = getWord[0];
     int index = firstChar - 'A';
    
     word *nextNewWord = new (nothrow) word;
     //wordFound = Find_In_List(letter, getWord);
     //if (!(wordFound))
    
    nextNewWord->newWord = getWord;
    nextNewWord->next = NULL;
        
    if(currentWord->head == NULL)
    {             
      letter[index]->newWord = getWord;
      word *next = NULL;
      word *back = NULL;
    }

    else
    {              
        nextNewWord->next = currentWord->head;
        currentWord->head = nextNewWord;
    }


}      
/*
bool Find_In_List(wordList *letter[], string getWord)
{
     
     bool wordFound = false;
     
     int firstLetter = getWord[0];
     
     for (int count = 0; count < SIZE_ALPHABET; count++)
     {    
         if (strcmp(letter[firstLetter], getWord) ) 
            wordFound = true;
         
     }   
     
    return wordFound;
} 
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.