Reputation: 573
I am working on a word cloud C++ program that is suppose to populate a list of words from a text file, put the words in a linked list, then sort the list by frequency and print the list with only one instance of the words and the frequency next to the word.
What I have accomplished is reading the file, populating the linked list, and printing it. However I cannot get the list to print from main instead of the wordCloud method loadWordCloud()
. I am not strong with classes yet, so I am sure that is my problem. But basically I want to call the printWordCloud()
method from main instead of printing from loadWordCloud()
. When I put getList.printWordCloud()
after I call getList.loadWordCloud(myFile)
, I get an empty list. I can see why that is happening, but I cannot figure out how to actually save the list from within loadWordCloud()
so I can print it later. I am trying to avoid a return statement and keep the method void.
Here is what I have for classes:
class wordNode{
public:
string myWord;
int freq_count;
bool black_list;
wordNode *next;
wordNode(string aWord);
~wordNode(void){};
};
wordNode::wordNode(string aWord){
myWord = aWord;
next = NULL;
freq_count = 0;
}
class wordCloud{
public:
wordNode *head;
int size;
wordNode *nextWord;
wordCloud(void);
~wordCloud(void){};
void insertWord(string aWord) { insertWord(aWord, false); }
void insertWord(string aWord, bool blacklist);
/*void insertWordDistinct(string aWord);
void loadBlacklist(string fileName);*/
void loadWordCloud(string fileName);
//void printBlacklist(void); // Print the words in the blackList
void printWordCloud() { printWordCloud(1); }
void printWordCloud(int freq); // Print the words with freq or greater freq_count
/*void Initialize(void) { nextWord = head; }
void GetNextWord(string theWord, int *freq_count);
void freqSort(string sortOrder); // 'A' = ASC, 'D' = DESC
private:
void properlyHandleHeadofList(string sortOrder);
void properlyHandleRestofList(string sortOrder);
string upperCase(string text);
string lowerCase(string text);*/
};
/*void wordCloud::GetNextWord(string theWord, int *freq_count){
wordCloud aWord;
aWord.insertWord(theWord);
}*/
wordCloud::wordCloud(void){
head = NULL;
size = 0;
nextWord = NULL;
}
void wordCloud::insertWord(string aWord, bool blacklist){
wordNode *newWord = new wordNode(aWord);
if (head == NULL)
head = newWord;
else{
newWord->next = head;
head = newWord;
}
size++;
}
void wordCloud::printWordCloud(int freq){
wordNode *temp;
if (head == NULL)
cout << "No Word Cloud" << endl;
else{
temp = head;
while (temp != NULL){
cout << temp->myWord << endl;
temp = temp->next;
}
}
system("pause");
}
void wordCloud::loadWordCloud(string fileName){
ifstream file; //variable for fileName
string word;
wordCloud newList;
//newList.Initialize();
file.open(fileName); //open file
while (!file.eof()){
file >> word; //grab a word from the file one at a time
transform(word.begin(), word.end(), word.begin(),
::tolower); //automatically change words to lowercase
newList.insertWord(word);
size++; //increment size
//cout << word <<'\n'; //print word - for debugging
}
newList.printWordCloud(); //print word cloud - debugging - works
cout << size << '\n'; //print size - for debugging - works
file.close();
system("pause");
}
And here is my main:
int main(){
string myFile = "words.txt";
wordCloud getList;
//For debugging
//list.insertWord("word");
//list.insertWord("more");
getList.loadWordCloud(myFile);
}
This program is far from done. I am also going to have to find out how do do the word frequencies, and delete a node when a word shows up twice, but for right now, I just want to know how I print from main. Any help would greatly be appreciated.
Upvotes: 1
Views: 107
Reputation: 28573
The problem is that in loadWordCloud
you have a local variable that you are loading into. Instead, you should be loading the current object. This way, the getList
variable would have the correct contents.
void wordCloud::loadWordCloud(string fileName)
{
ifstream file; //variable for fileName
string word;
file.open(fileName); //open file
while (!file.eof()){
file >> word; //grab a word from the file one at a time
transform(word.begin(), word.end(), word.begin(),
::tolower); //automatically change words to lowercase
insertWord(word);
//Don't increment size here, insertWord does this! size++;
//cout << word <<'\n'; //print word - for debugging
}
printWordCloud(); //print word cloud - debugging - works
cout << size << '\n'; //print size - for debugging - works
file.close();
system("pause");
}
You also need to remove the size++
, since insertWord
will do this for you (otherwise you get size
double the quantity of words).
If you want to call loadWordCloud
multiple times and want to clear the list instead of adding, you should add a function to clear the list and call delete
on all of the nodes. You should also re-use that code for your destructor.
Upvotes: 1