【问题标题】:Comparison between supposedly identical strings failing ' == ' comparison假设相同的字符串之间的比较失败'=='比较
【发布时间】:2019-04-16 15:42:58
【问题描述】:

我正在尝试将字典文件读入单词对象的向量,然后我会对其进行迭代并将对象的单词与用户输入的单词进行比较。

但是,当在 Dictionary::wordFind() 中与应该与单词对象的单词相同的单词(例如“aa”)进行比较时,无法正确进行比较。

没有错误输出,wordFind() 中的 if 语句由于某种原因没有实现。

字典.txt

aa
acronym for Associate in Arts a college degree granted for successful completion of a two-year course of study in arts or general topics;  Alcoholics Anonymous.
n

aaas
the American Association for the Advancement of Science an organization with headquarters in Washington D.C..
n

字典.cpp

void Dictionary::loadDictionary() {

    ifstream dicFile("dictionary.txt");
    string word, def, type, whitespace;

    if (dicFile.is_open())
    {
        while (!dicFile.eof())
        {
            getline(dicFile, word);
            getline(dicFile, def);
            getline(dicFile, type);
            getline(dicFile, whitespace);
            Word word1(word, def, type);
            wordObjects.push_back(word1);

        }
        dicFile.close();
    }
}

void Dictionary::wordFind(string wordToFind) {

    for (Word test : wordObjects)
    {
        if (test.getWord() == wordToFind)
        {
            cout << "Word found!" << endl;
            cout << "Word: " << test.getWord() << "\n\n" << "Definition: " << "\n" << test.getDef();
        }
    }
    cout << "Word not found" << endl;
}

word.cpp

Word::Word(string _word, string _def, string _type) {
    word = _word;
    def = _def;
    type = _type;
}

string Word::getWord() {
    return word;
}

string Word::getDef() {
    return def;
}

string Word::getType() {
    return type;
}

main.cpp

int main()
{
    Dictionary dic;
    dic.loadDictionary();
    if (menuChoice == 1)
    {
        string wordSearch;
        cout << "Please enter your word: " << endl;
        cin >> wordSearch;
        dic.wordFind(wordSearch);
}

我注意到使用cout &lt;&lt; wordObjects[2].showWord();(它将输出单词“aaas”,如上面的dictionary.txt 所示),输出似乎在单词的字母之间有空格,如下面的链接所示。 (我试图只添加图像,但我没有足够的业力。相信我,它不会是一个讨厌的链接) https://i.ibb.co/t4053Tf/12221222222222121212.png

我不确定为什么会发生这种情况,我想知道是否有人知道我的代码为什么会产生这种行为。

非常感谢任何建议!

编辑:感谢 Paul Sanders 关于 Unicode 字符的评论,我重新创建了 dictionary.txt,但将其保存在 ANSI 中,它似乎解决了我的问题。谢谢!

【问题讨论】:

  • 欢迎来到 Stack Overflow!听起来您可能需要学习如何使用调试器来逐步执行代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。进一步阅读:How to debug small programsDebugging Guide
  • 听起来像dictionary.txt 可能包含Unicode。你是如何创建它的?
  • 它是由我的讲师创建的。我从将 dictionary.txt 加载到页面的网页中复制了文本。当我将其保存到文本文件时,它会警告我该文本是 Unicode 并且我正在尝试以 ANSI 格式保存。
  • 我刚刚重新创建了 dictionary.txt,但将其保存在 ANSI 中,它似乎解决了我的问题。非常感谢@PaulSanders
  • 这并没有解决问题,但您可以通过删除 if (dicFile.is_open()) 和相应的大括号、将 while (!dicFile.eof()) 更改为 while (dicFile) 并删除 @987654334 来简化 Dictionary::loadDictionary() 中的代码@。只要文件处于有效状态,while (dicFile) 就会循环。这包括打到文件末尾和一开始就无法打开。并且不需要关闭文件——析构函数会这样做。

标签: c++ visual-studio visual-c++


【解决方案1】:

由于您使用的是 getline,虽然这可能不是您的问题,但您的单词后面可能会有空格,这些空格也会成为您字典的一部分。从文件中读取时,您应该使用空格分隔符。

我建议先在你的控制台输出一个字典单词,这样你就知道它被正确存储了。

【讨论】:

    猜你喜欢
    • 2016-12-05
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多