繁体   English   中英

我如何保留变量以进行比较

[英]How can I keep the variable to be used to compare

您好,即时通讯不知道如何保存变量,让我们说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();     
}

如何将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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM