【发布时间】:2018-09-30 07:40:36
【问题描述】:
我的程序应该接受一些输入,例如“你好”。或“我有一个”。 (注意结束点)如果字符串包含“a”则输出“yes”,如果不包含则输出“no”。 问题是 cin 会跳过空格,而 noskipws 似乎不起作用。
我得到的代码:
#include <iostream>
#include <string>
using namespace std;
int main() {
string sequence;
cin >> noskipws >> sequence;
for (auto c : sequence) {
if (c == 'a') {
cout << "yes" << "\n";
return 0;
}
}
cout << "no" << "\n";
}
Input: "where are you." Output: nothing
Input: "what." Output: yes
【问题讨论】:
-
使用
std::getline() -
这里有没有人解释为什么
noskipws在OP的例子中不起作用,为什么std::getline()可以解决它?