我不能接受getline

I cannot cin with getline

本文关键字:getline 不能接受      更新时间:2023-10-16
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 合作。编译器只是跳过此步骤并将向量零设置为默认值

在调用之前使用虚拟getchar()cin.get()消耗额外的n getline()

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