【发布时间】:2015-04-17 13:23:25
【问题描述】:
#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;
}
输出:
Input an integer a= 1
1
Input a string str=
End program
fflush() 之后的getline 不起作用。
【问题讨论】:
-
我不知道答案,但这不完全是 getline() 问题。如果不先阅读
num,getline() 就可以了。 -
尝试使用
getchar();而不是fflush(stdin); -
@Cool Guy:谢谢,伙计 :)
-
不知道为什么这个问题会出现在审核队列中,但我做了一些修复以将其排除。