问个小问题
#include <iostream>#include <string>using namespace std;int main(){ string strInput; cin >> strInput; string strBuf; cout << strBuf << endl; char buf[20] = {0}; cin.getline(buf,20); //为什么不能再次输入字符串,而直接跳过这一句。。 cout << buf << endl; return 0;}#include <iostream>#include <string>using namespace std;int main(){ string strInput; cin >> strInput; string strBuf; cout << strBuf << endl; cin.get(); //吸收掉 上面 cin>>strInput输入时的回车字符 char buf[20] = {0}; cin.getline(buf,20); //为什么不能再次输入字符串,而直接跳过这一句。。 cout << buf << endl; return 0;}
[解决办法]
#include <iostream>
#include <string>
using namespace std;
int main()
{
string strInput;
cin >> strInput;
string strBuf;
cout << strBuf << endl;
cin.sync();
char buf[20] = {0};
cin.getline(buf,20);
cout << buf << endl;
return 0;
}