简体   繁体   中英

How can I keep the variable to be used to compare

Hello im not sure how I can keep the variable lets say lastWordLoaded and then use this to compare with the next word being loaded in the dictionary which would be word ..

I have a method compareWords that will print out -1 if the lastWordLoaded > CurrentWord, thus this would mean its not in alphabetical order, if lastWordLoaded < CurrentWord it will print 0, thus if 0 the file is in alphabetical order. Though I'm unsure how I can retreive the lastWordLoaded to be used in this situation.

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();     
}

What about keeping the lastWordLoaded in a variable and updating it at the end of the cycle like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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