验证C ++中两个字符串是否相等

时间:2014-02-02 20:29:18

标签: c++ string function boolean

我正在做一个要求我使用函数检查两个字符串是否相等的赋值。我一直在第20行得到一个解析错误,这里调用了函数,我不知道出了什么问题。如果您发现可能导致问题的原因,请查看并告诉我。谢谢!

#include <iostream>
#include <string>

using namespace std;

bool checker(string firstWordParameter, string secondWordParameter);

int main()
{
    string firstWord, secondWord;
    bool match;

    cout << "Hello user.\n"
         << "This program will determine whether two words are the same.\n"
         << "Please enter your first word you would like to check: ";
    getline(cin, firstWord);
    cout << "Great, now enter the second word: ";
    getline(cin, secondWord);

    match = bool checker(firstWord, secondWord);

    if(match == true){
        cout << "Match.";
    }else{
        cout << "Totally not a match.";
    }

    return 0; 
}

bool checker(string firstWordParameter, string secondWordParameter)
{
    if(firstWordParameter == secondWordParameter){
        return true;
    }else{
        return false;
    }
}

2 个答案:

答案 0 :(得分:7)

尝试更改

match = bool checker(firstWord, secondWord);

match = checker(firstWord, secondWord);

答案 1 :(得分:5)

第20行

 match = bool checker(firstWord, secondWord);

将其更改为

match = checker(firstWord, secondWord);

此外,当您在编译器中看到错误时,双击它然后它会显示错误行。