C ++将basic_string转换为bool时出错

时间:2016-06-29 02:14:46

标签: c++ string boolean

您好这可能是一个简单的问题,我很抱歉为这么简单的问题消耗时间,但我正在努力学习c ++。以下是我正在练习的一个小程序。我在代码中遇到了障碍,因为当我执行它时,我收到以下错误:“在函数'int main()'中:

20:19:错误:不匹配'运营商!' (操作数类型是'const string {aka const std :: basic_string}') 20:19:注意:候选人是: 20:19:注意:操作员!(布尔) 20:19:注意:参数1从'const string {aka const std :: basic_string}'到'bool'没有已知的转换

下面你会发现我的所有代码谢谢你的帮助!         #包括         #include

    using namespace std;

int main()
{
const string go = "tomato";
string Answer;

cout << "What is your friends favorite food?" << endl;
cout << "ENTER HERE:" << " " << flush;
cin >> Answer;


if(Answer == go)
{
    cout << "Congratulations you know your friend!!!" << endl;
}
while(Answer =! go)
{
    cout << "What is your friends favorite food?" << endl;
    cout << "ENTER HERE:" << " " << flush;
    cin >> Answer;

    cout << "You do not know your friend please try again!!!" << endl;


}






return 0;

}

3 个答案:

答案 0 :(得分:0)

不等式比较运算符为!=,而不是=!

答案 1 :(得分:0)

将您的代码更改为此(特别是将=!更改为!=

    using namespace std;

int main()
{
const string go = "tomato";
string Answer;

cout << "What is your friends favorite food?" << endl;
cout << "ENTER HERE:" << " " << flush;
cin >> Answer;


if(Answer == go)
{
    cout << "Congratulations you know your friend!!!" << endl;
}
while(Answer != go)
{
    cout << "What is your friends favorite food?" << endl;
    cout << "ENTER HERE:" << " " << flush;
    cin >> Answer;

    cout << "You do not know your friend please try again!!!" << endl;


}






return 0;

答案 2 :(得分:0)

Answer =! go表示Answer = (!go)go的否定未定义,因为go不是bool。

您可能想要的是“不相等”的比较,即!=这两个字符的序列是重要的。