0

I am trying to take user input (strings) and store them in a vector . but this while cin doesn't stop taking user input. I know there is a getline() function etc. also but why does while getline or while cin dont stop taking user input? any solution?

string word;
   vector <string> text;

   while (cin >> word) {
      text.push_back(word);
   }
Mar Cos
  • 1
  • 1
  • 2
    When do you want it to stop? See dupe: [What's the difference between while(cin) and while(cin >> num)](https://stackoverflow.com/questions/19483126/whats-the-difference-between-whilecin-and-whilecin-num). Here is another: [How is "std::cin>>value" evaluated in a while loop?](https://stackoverflow.com/questions/34395801/how-is-stdcinvalue-evaluated-in-a-while-loop) – Jason Oct 19 '22 at 09:10
  • when i hit the enter? – Mar Cos Oct 19 '22 at 09:10
  • You loop will read until there's either an error, or there an end-of-file condition. – Some programmer dude Oct 19 '22 at 09:11
  • If you want to read a line, use `std::getline`. If you then want to split the line into "words", then put the line into an `std::istringstream` and use your loop to get the words from that stream instead. – Some programmer dude Oct 19 '22 at 09:11
  • got my answer. ctrl+Z can solve the problem. – Mar Cos Oct 19 '22 at 09:18
  • Note that `Ctrl-Z` set the `eofbit` flag. This means you can't read more input until you clear that flag. – Some programmer dude Oct 19 '22 at 09:22

0 Answers0