C++中输入一个包含空格和换行的字符串如何结束输入如题[解决办法]C/C++ code#include iostream#include
C++中输入一个包含空格和换行的字符串如何结束输入
如题
[解决办法]
- C/C++ code
#include <iostream>#include <string>using namespace std;int main(void){ string str; getline(cin,str); char *p; p = new char[str.size() + 2]; strcpy(p,str.c_str()); p[str.size()] = '\n'; p[str.size()+1] = '\0'; cout << p; return 0;}
[解决办法]
用EOF代表结束,Ctrl + D或者Ctrl + Z(这两种组合有一个是的)按键结束 