Some question about user input

I am currently working on my C++ assignment and i have a question

The user of this program will input a text which contains many English word separated by spacing and line break. The program will count the word appearing in the text. I got most of the job done except the beginning part.

I use getline to get the input and stored it into t.
if the text is only one line, the program can run smoothly. But if the text contains line break, it can only read the first line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
    string t , userinput;

    string textcount [100] ;

    getline(cin,t) ;

    string word="";

    int numofword = 0 ;

    for(int i = 0 ; i <= t.size() ; i++){

        if( t[i] == ' ' || i == t.size())

        {
            textcount[numofword] = word ;

            

            word = "" ;

                numofword = numofword + 1 ;

        } else {

            word = word + t[i] ;

        } ;

        

    }


How can i modify the program in order to correctly get text with line break?

Here is one of the example of the input


Computer system
computer design
algorithm design and analysis
quantum computer
computer science department
1
2
3
std::vector<std::string> words;
for (std::string word; std::cin >> word;)
    words.emplace_back(std::move(word));
if the text is only one line, the program can run smoothly. But if the text contains line break, it can only read the first line.


getline(cin,t) ; is out of any loops, so it will be executed only once.
Topic archived. No new replies allowed.