I am trying to print the input and then all the strings in the tk vector, as in the following program:
int main() {
while (true) {
string input;
cout << "prompt: ";
cin >> input;
vector<string> tk = {"this", "is", "an", "example"};
cout << input << endl;
for (int i = 0; i < tk.size(); i++) {
cout << tk[i] << endl;
}
}
return 0;
}
When I give the input "Hello world" I am expecting the output to be:
Hello world
this
is
an
example
prompt:
But the output was:
Hello
this
is
an
example
prompt: world
this
is
an
example
prompt:
Does anyone know what went wrong here? I guess the cause is related to how the buffer works, but I really have no idea about the details.