Dictionary::Dictionary() {
wordCount = 0;
dictionaryFileName = "dictionary.txt";
//attempt to open the file
ifstream dictionaryFile(dictionaryFileName);
//check if the file was opened
if (dictionaryFile.is_open()) {
//start a timer (clock_t is normaly a 'long')
clock_t start = clock();
//load the file
cout << "Loading the dictionary";
string word, definition, type, blank;
Word* fromRecord(const string &theWord,
const string &theDefinition,
const string &theType)
{
if (theType.compare("v") == 0) {
return new Verb(theWord, theDefinition);
}
else if (theType.compare("n") == 0) {
return new Noun(theWord, theDefinition);
}
else if (theType.compare("adv") == 0) {
return new adVerb(theWord, theDefinition);
}
else
{
cout << "your error message";
return NULL;
}
}
while (getline(dictionaryFile, word) &&
getline(dictionaryFile, definition) &&
getline(dictionaryFile, type) &&
getline(dictionaryFile, blank))
{
myWords[wordCount] = fromRecord(word, definition, type);
wordCount++;
}
//show a progress bar
if (wordCount % (MAX_WORDS / 10) == 0) {
cout << '.';
}
//close the file
dictionaryFile.close();
//let the user know what happend
double duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
//measure duration
cout << endl << "Done (" << wordCount << " words in " << duration << "seconds)." << endl << endl;
string searchWord = myWords[wordCount / 2]->getWord();
//search for the word
cout << "Testing word search for: " << searchWord << endl;
start = std::clock(); //restart the clock
int wordIndex = 0;
while (myWords[wordIndex]->getWord().compare(searchWord) != 0) {
wordIndex++;
}
//show the result of the search
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC; //measureduration
cout << "(in " << duration << "seconds) " <<
myWords[wordIndex]->getDefinition() << endl << endl;
}
else {
//normally this means the file was not found, copy the file to yoursolution!
cout << "ERROR: Could not open " << dictionaryFileName << endl;
}
}