[英]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.