0

How does one go about having a user input a sentence, and the program picking the words apart and putting each word into a vector. I've tried something like this, but it ends up stalling:

vector<string> sentence;
string nextWord = "notyetdefined";

cout << "Type your sentence:" << endl;
while (nextWord != "" && nextWord != "\n") {
    cin >> nextWord;
    sentence.push_back(nextWord);
}

I've also tried detecting for a period at the end of the sentence which works, but is annoying that the user would have to put a period everytime, because they might often forget, resulting in an endless loop that I'm not sure how to deal with.

EDIT: Managed to find a fix by using this question: Split a string in C++? that was suggested to me in the comments.

Community
  • 1
  • 1
Jacob Oaks
  • 140
  • 2
  • 11

0 Answers0