首页 C/C++修行正文

C++ cin无法输入空格

欲儿 C/C++修行 2019-08-03 437 0

其实呢,不是无法输入空格,是可以输入空格,只不过你输入了以后字符串就被分割了,所以要用到getline函数,



我们先看看原来不能输入空格的代码

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string temp;
	cin >> temp;
	cout << temp << endl;

	while (1);
	return 0;
}


利用getline函数完美解决这个问题

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string temp;
	getline(cin, temp);
	cout << temp << endl;

	while (1);
	return 0;
}


版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论