我如何告诉cin不允许输入空格?(c++)
How do I tell cin to not allow spaces on input? (C++)
当这运行时,我想不能在cin
中输入任何空格,我可以为此使用什么格式说明符?
在<string>
中有什么东西可以让我这样做吗?
#include <iostream>
#include <string>
using namespace std;
int main(){
string yy;
cin >> string;
/*when this runs I want to not be able to enter any spaces in `cin`
what format specifier can I use for this? is their anything in `<string>` that lets me do this?
*/
return 0;
}
您可以使用std::noskipws
来禁用在格式化输入之前自动跳过空白:
if (std::cin >> std::noskipws >> yy) {
std::cout << "read '" << yy << "'n";
}
如果用户在字符串之前输入空格,则会出现错误,因为没有单词可以成功读取。
相关文章:
- 2D数组来自文本输入,中间有空格
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- 如何在c++中使用system()来运行包含空格的python脚本
- basic_string的前导/尾部不区分空格的特征
- 移除前面有空格的前2个字符串
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 为什么 c++(g++) 不允许模板返回类型和函数名称之间有空格?
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 在字符串中插入空格
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- C++为一串单词添加空格
- 使用另一个字符串从字符串中删除空格
- 我还可以添加什么来按空格或空格正确拆分C++字符串?
- 如何打印不带空格的输入整数?
- 野牛/yacc 解析器在不被空格分隔时跳过 grammer - "unexpected $end"
- C++ "noskipws"没有按预期工作,如何正确允许字符串中的空格?
- 在 c++ 中逐行阅读字符串,不要以空格停止