C++ 使用cin处理空白和返回(换行符)

C++ 使用cin处理空白和返回(换行符),c++,newline,whitespace,cin,C++,Newline,Whitespace,Cin,如果程序希望用户输入文本(例如名称),如何处理空白和返回键 charch[100]; cout>ch; 不能像这样: #include <iostream> #include <string> int main() { for (std::string line; std::getline(std::cin, line); ) { if (line.empty()) { // handle blan

如果程序希望用户输入文本(例如名称),如何处理空白和返回键

charch[100];
cout>ch;
不能像这样:

#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";
    }
}
#包括
#包括
int main()
{
for(std::string-line;std::getline(std::cin,line);)
{
if(line.empty())
{
//处理空白输入
}

std::cout@user2365568:No.使用我提供的链接并使用
trim
功能。
#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";
    }
}