![](/img/trans.png)
[英]how can I compare an instance of a class with a char * variable in c++?
[英]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.