0

您好,我不确定如何保留变量让我们说lastWordLoaded,然后使用它与字典中加载的下一个单词进行比较,这将是word..

我有一个方法compareWords,如果 lastWordLoaded > CurrentWord 将打印出 -1,因此这意味着它不是按字母顺序排列的,如果 lastWordLoaded < 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();     
}
4

1 回答 1

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;
    }
于 2013-10-25T08:27:32.343 回答