-1

I have a simple question but any one makes me known. below there is simple code.

#include <iostream>
#include <string>
using namespace std;

int main(){
 string ans;
 while(true){
  cin >> ans;
  cout << "ans : " << ans << endl;
 }
}

I type usa canada england island and enter !!

It shows result below

usa canada england island

ans : usa

ans : canada

ans : england

ans : island

hello world

ans : hello

ans : world

Why does it skip second cin ?? How does it work in stream buffer?

Please answer my question thank you

leiyc
  • 903
  • 11
  • 23
Rihun Kim
  • 1
  • 1
  • 1
    Is your question "I enter multiple words, separated by spaces and `cin` is skipped for all those words entered? Are you expecting it to print "usa canada england island" first, then "hello world". You should clarify your question, it's a little difficult to read. – Tas Dec 13 '18 at 05:28
  • I have to agree with Tas, you should clarify your question. – OpticalMagician Dec 13 '18 at 05:37
  • Possible duplicate of [Program skips second cin](https://stackoverflow.com/questions/19390059/program-skips-second-cin) – Tas Dec 13 '18 at 05:47

1 Answers1

0

I think you might want to use std::getline if your goal is to output the all the inputs they entered rather than each item being seperated.

int main(){
  string ans;
  while(true){
    getline(cin, ans);
    cout << "ans : " << ans << endl;
  }
}
donpsabance
  • 358
  • 3
  • 9