If I type john when prompted for a char, the while statement will loop 4 times, one for each letter of john, before it asks again for user input.
Why does this program do not allow me insert more input before the whole 4 chars of john are consumed ? I would expect it to discard the 3 remaining letters of the string john and asked me for more input on the second loop.
The whole example can be found at page 44 of Bjarne Stroustrup The C++ Programming Language 4th edition.
#include <iostream>
using namespace std;
bool question() {
while (true) {
cout << "Continue ?\n";
char answer = 0;
cin >> answer;
cout << "answer: " << answer << endl;
}
return false;
}
int main () {
cout << question() << endl;
}
The output becomes:
Continue ?
john
answer: j
Continue ?
answer: o
Continue ?
answer: h
Continue ?
answer: n
Continue ?