int main() { bool done{false}; ActionT action{ActionT::QUIT}; WordT words[MAX_WORDS]; size_t wordCount{0};
void ReadWords( WordT words[], size_t & wordCount);
inFile >> word; while (inFile) { words[wordCount].SetWord(word); wordCount++; inFile >> word; }
inFile >> word; while (inFile) { if(wordCount < MAX_WORDS) { words[wordCount].SetWord(word); wordCount++; } else { // error message about array capacity } inFile >> word; }
void PrintWords(const WordT words[], size_t wordCount){ size_t i; for(i = 0; i < wordCount; i++) { cout << setw(20) << words[i].GetWord() << setw(10) << words[i].GetCount() << endl; } cout << endl; }
ReadWords(words, wordCount); while (not done) { PrintWords(words, wordCount); action = GetUserAction(); PerformUserAction(action, done); }
FixWords(string & word)
InsertWord
void InsertWord(string word, WordT words[], size_t & wordCount) { FixWord(word); if (word != "") { if (wordCount < MAX_WORDS) { words[wordCount].SetWord(word); wordCount++; } else { cout << "The array is full, can not insert another word" << endl; } } return; }
void ReadWords( WordT words[], size_t & wordCount){ string fileName; ifstream inFile; string word; fileName = GetFileName(); inFile.open(fileName); if (inFile) { wordCount = 0; inFile >> word; while (inFile) { InsertWord(word, words, wordCount); inFile >> word; } inFile.close(); } return; }
size_t FindWord(WordT words[], size_t wordCount, string key) { size_t i; i = 0; while (i < wordCount and words[i].GetWord() != key) { i++; } return i; }
void InsertWord(string word, WordT words[], size_t & wordCount) { size_t pos; FixWord(word); if (word != "") { pos = FindWord(words, wordCount, word); if (pos < wordCount) { words[pos].IncrementCount(); } else if (wordCount < MAX_WORDS) { words[wordCount].SetWord(word); wordCount++; } else { cout << "The array is full, can not insert another word" << endl; } } return; }