如何在CIN中以C 的CIN中的所有读取为字符串

How to read in all whitespaces with cin in C++ into a string?

本文关键字:CIN 读取 字符串 中以      更新时间:2023-10-16

是一种令人费解的问题,但是是否可以从用户读取输入,而是让CIN在Newlines或Whitespaces上停止?例如,用户可以输入:

Hello  w orld!

Hello World!

,我希望所有的空格和纽线字符输入到字符串中,只是用户的原始输入。有人可以告诉我如何在C 中做到这一点吗?谢谢。

您可以使用std :: getline为此:

#include <iostream>
#include <string>
int main()
{
  std::string helloWorld;
  std::getline( std::cin, helloWorld );
  return 0;
}