简体   繁体   English

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

[英]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 .. 您好,即时通讯不知道如何保存变量,让我们说lastWordLoaded ,然后使用它与字典中正在加载的下一个单词进行比较,即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. 我有一个compareWords方法,如果lastWordLoaded> CurrentWord,它将打印出-1,因此这意味着它不是按字母顺序排列的;如果lastWordLoaded <CurrentWord,它将打印0,因此,如果文件为按字母顺序排列为0。 Though I'm unsure how I can retreive the lastWordLoaded to be used in this situation. 尽管我不确定如何才能在这种情况下使用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();     
}

What about keeping the lastWordLoaded in a variable and updating it at the end of the cycle like this: 如何将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.

相关问题 如何在C ++中将类的实例与char *变量进行比较? - how can I compare an instance of a class with a char * variable in c++? 如何简洁地将单个变量与许多不同的值进行比较? - How can I concisely compare a single variable to many different values? 我不断获取用于 interestRate、numberOfPayments 和 loanAmount 的单元化变量 - I keep getting unitialized variable used for interestRate, numberOfPayments, and loanAmount 如何为数组中的原子变量调用 compare_exchange_weak(0,1)? - how can i call compare_exchange_weak(0,1) for an atomic variable in an array? 如何为同一个类对象的成员函数保留单独的变量副本? - How can I keep separate variable copy for same class object's member function? 在 do while 循环后如何保持变量的值? - How can I do to keep the value of a variable after a do while loop? 变量名使用什么数据类型? 以及如何快捷地为多个变量赋值? C++ - What data type is used on variable names? And how can I shortcut assignments to multiple variables? C++ 如果重用变量值,如何将变量保持在常量之下? - How do I keep variable below a constant if the variable value is reused? 我不断从以前使用的工作代码中收到“错误:constexpr 变量'x'必须由常量表达式初始化” - I keep getting an "error: constexpr variable 'x' must be initialized by a constant expression" from previously used working code 如何访问和比较数据? - How can I reach and compare the data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM