#include <iostream>
#include <string>
using namespace std;
int main(){
int num;
string str;
cout << "Input an integer a= ";
cin >> num;
cout << num << endl;
cout << "Input a string str= ";
fflush(stdin);
getline(cin,str);
cout << str << endl;
cout << "End program" << endl;
return 0;
}
输出工作:函数getline后不fflush
Input an integer a= 1
1
Input a string str=
End program
fflush后getline
()不工作。
我不知道答案,但它不完全是getline()问题。如果你没有先读取'num',getline()就可以了。 –
尝试使用'getchar();'而不是'fflush(stdin);' –
@酷人:谢谢,人:) – Lawliet