What I'm trying to do is read in a line from a text file, break it down into the words that compose it, and then check each word against a list of "bad words" that I don't want to hash. Each "good word" that isn't in the list of bad words should get hashed and store the entire line at its index (if that makes sense). So, for example, "Ring of Fire" would be split into "Ring", "of", and "Fire". I would hash "Ring" and store "Ring of Fire" with it, I would see "of" and notice that its a bad word and skip it, and finally I would hash "Fire" and store "Ring of Fire" with it as well.
My code as is separates a line into words, compares it with bad words, and displays all good words. It then closes the file, reopens it, and displays all of the lines. What I am having trouble conceptualizing is how to combine the two to hash all the good words and the entire line at the same time so that I can store them easily. How should I go about doing this?
#include <cstring>
#include <cctype>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const char * bad_words[] = {"of", "the", "a", "for", "to", "in", "it", "on", "and"};
ifstream file;
file.open("songs.txt");
//if(!file.is_open()) return;
char word[50];
while(file >> word)
{
// if word == bad word, dont hash
// else hash and store it in my hash table
bool badword = false;
for(int i = 0; i < 9; ++i)
{
if(strcmp(word, bad_words[i]) == 0)
{
badword = true;
}
}
if(badword) continue;
else
{
// get all words in a line that are not in bad_words
char * good_word = new char[strlen(word)+1];
strcpy(good_word, word);
cout << good_word << endl; // testing to see if works
// hash each good_word, store good_line in both of them
//int index = Hash(good_word);
//Add(good_line) @ table[index];
}
}
file.close();
file.open("songs.txt");
while(!file.eof()) // go through file, grab each whole line. store it under the hash of good_word (above)
{
char line[50];
file.getline(line, 50, '\n');
char * good_line = new char[strlen(line)+1];
strcpy(good_line, line);
cout << good_line << endl; // testing to see if works
}
return 0;
}