我无法使用getline

时间:2017-10-22 19:30:46

标签: c++ cin getline

int itrtn;
cout << "How many iterations? ";
cin >> itrtn;

double leftx, rightx,midx,midy;
cout << "Enter the endpoints of the interval containing the root: ";
cin >> leftx>>rightx;

cout<<"Enter the polynomial coefficients, ascending in degree: ";
string degree;
getline(cin,degree); // gets an entire line of input 
istringstream fake_keyboard(degree); // use fake keyboard like cin 
vector<double> coefficients;
double coeff;
while (fake_keyboard >> coeff) coefficients.push_back(coeff);

这是我的代码,但我无法使用getline来解决第三个问题。编译器只是跳过此步骤并将向量零设置为默认值

1 个答案:

答案 0 :(得分:1)

getchar()来电之前,使用虚拟cin.get()\n消费额外getline()

cin >> leftx>>rightx;
cin.get(); //consume the '\n' of previous input.
cout<<"Enter the polynomial coefficients, ascending in degree: ";