-1
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);

this is my code, but i cannot cin with getline for the third question. The compiler just skips this step and set the vector zero as default

Yuchen Zhang
  • 31
  • 1
  • 4
  • 1
    What is the purpose of `fake_keyboard`? Maybe you need *real* keyboard. – tadman Oct 22 '17 at 19:32
  • After you give the input for `rightx`, what key do you use to "deliver" the input to your program? The `Enter` key? The one that results in a newline being added to the input buffer? Now think a little while what [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline) by defaults look for? – Some programmer dude Oct 22 '17 at 19:35

1 Answers1

1

Use a dummy getchar() or cin.get() to consume extra \n before getline() call.

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