您好我不知道如何保留变量让我们说lastWordLoaded
然后用它来比较字典中加载的下一个单词word
..
我有一个方法compareWords
如果lastWordLoaded>将打印出-1如果lastWordLoaded< CurrentWord,那么这将意味着它不是按字母顺序排列的。 CurrentWord它将打印0,因此如果0,则文件按字母顺序排列。虽然我不确定如何在这种情况下检索lastWordLoaded
。
Dictionary::Dictionary() {
//check if the file was opened
if (dictionaryFile.is_open()) {
while (getline(dictionaryFile, word) &&
getline(dictionaryFile, definition) &&
getline(dictionaryFile, type) &&
getline(dictionaryFile, blank)) {
myWords[wordCount] = fromRecord(word, definition, type);
if (compareWords(word, lastWordLoaded) != 0)
{
cout << "\nThe dictionary isnt in alphabetical order." << endl;
cout << "Error at word = " << getWordCount() << endl;
system("Pause");
}
}
//close the file
dictionaryFile.close();
}
答案 0 :(得分:0)
如何将lastWordLoaded
保留在变量中并在循环结束时更新它,如下所示:
string lastWordLoaded = "";
while (getline(dictionaryFile, word) &&
getline(dictionaryFile, definition) &&
getline(dictionaryFile, type) &&
getline(dictionaryFile, blank)) {
myWords[wordCount] = fromRecord(word, definition, type);
if (compareWords(word, lastWordLoaded) != 0)
{
cout << "\nThe dictionary isnt in alphabetical order." << endl;
cout << "Error at word = " << getWordCount() << endl;
system("pause");
}
lastWordLoaded = word;
}