如何保持用于比较的变量

时间:2013-10-25 08:16:21

标签: c++

您好我不知道如何保留变量让我们说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();     
}

1 个答案:

答案 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;
    }