用cin处理空格并返回(换行符)
Handle whitespace and return (newline) with cin
如果程序希望用户输入文本(例如名称),如何处理空白和返回键?
char ch[100];
cout << "enter something: ";
cin >> ch;
cout << ch << endl;
如果用户
- 回车
- 空格+回车
像这样:
#include <iostream>
#include <string>
int main()
{
for (std::string line; std::getline(std::cin, line); )
{
if (line.empty())
{
// handle blank input
}
std::cout << "You said: " << line << "n";
}
}
这还不修剪空白;这可以通过在循环开始时修改line
来轻松完成,例如使用此代码并在开始时添加trim(line);
。
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- C/C++ - 查询平台相关的换行符(用于内存映射文件)
- 在 Stream C++ 文本之前有一个额外的换行符
- 流:CSV 文件中的换行符
- 如何在三元条件运算符中添加换行符和连接? :在 C++ 中
- Clang-格式:在多行语句之后的换行符上打开大括号
- 在输出流中插入换行符
- 防止控制台在通道字符(在 c++ 中)中输入空格(即空格、制表符和换行符)
- QProcess::readAllStandardOutput() 和换行符
- 如果有换行符,clang 格式不会附加大括号
- 提取后返回换行符的C++istream.get()
- 如何在出现换行符之前将多个整数作为输入?
- 正则表达式 获取两个换行符之间的文本
- 为什么 std::cout 在我的输出中添加返回或换行符
- 当子字符串只是一个换行符时,string.find(substring) 函数返回什么
- 为什么 fstream::tellg() 返回值会因输入文本文件中的换行符数而增大,当文件针对 Windows (r
- 返回Rcpp中size_t的换行符
- 修改cin以返回换行符
- 用cin处理空格并返回(换行符)
- Boost::asio::async_read返回文件结束错误的换行符