c ++中if条件中字符串比较的问题

问题描述

我正在尝试输入一个带有空格的字符串,直到文件末尾。然后我将它与 if 条件中的另一个字符串进行比较 条件表示,如果字符串匹配,则打印 True,否则打印 False。但是我的代码有问题。根据我的输入,所有输出都应该是“真”。但它仅在最后一行打印 true。 这里出了什么问题?

代码

#include<bits/stdc++.h>
using namespace std;

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    
    string s;

    while(getline(cin,s)){
        if(s == "my name is corey")
            cout<<"True"<<endl;
        else
            cout<<"false"<<endl;
    }
}

输入:

my name is corey
my name is corey
my name is corey
my name is corey

输出

false
false
false
True

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)