C++ cin无法输入空格 2019-08-03 其实呢,不是无法输入空格,是可以输入空格,只不过你输入了以后字符串就被分割了,所以要用到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; }
发表评论: