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来解决第三个问题。编译器只是跳过此步骤并将向量零设置为默认值
答案 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: ";