c++ - 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
解决方案
推荐阅读
- c# - 使用 Wordpress 调试 Asp net core oauth 登录并正确注销
- reactjs - 无法导入 css 文件说错误意外令牌
- python - Python Bash 脚本启动
- azure - Azure Function Service Bus Trigger throws error 启动操作期间发生主机错误
- python - 非线性测试 NaN 错误
- rust - 丢下一个 tokio::pin!-ned 异步块不释放借用
- spring - 如何在 Spring Security 中扩展登录用户信息?
- javascript - Node.js 内部 500 服务器错误“无法读取未定义的 id”
- swift - UIActivityIndicator 不显示
- java - Spring-Maven 应用程序无法执行“mvn spring-boot:run”