How to read multiple rows in c ++ without using a file

-1

I need to make a program in c ++ that reads a multi-line input and stores each line (string) in a vector. I've already tried using cin.eof() and fgets() but it did not work.

#include<iostream> 
#include<string> 

using namespace std; 

int main()
{ 
    string entrada; 
    cin >> entrada; 
    while(!cin.eof())
    {
        cout << "Entrada ainda não terminou." << endl; 
        cin.ignore(); 
        getline(cin, entrada); 
        cout << entrada << endl; 
    } 

    cout << "Entrada terminou." << endl; 
    return 0; 
}

Can anyone help?

    
asked by anonymous 29.05.2017 / 23:39

1 answer

2

I've changed your code a bit to get one more result inside what I imagine you want to do:

#include<iostream> 
#include<string> 

using namespace std; 

int main()
{ 
    string entrada; 
    getline(cin, entrada); 
    cout << entrada << endl; 

    do
    {
        cout << "Entrada ainda não terminou." << endl; 
        // cin.ignore(); 
        getline(cin, entrada); 
        cout << entrada << endl;     
    } while(entrada != "");

    cout << "Entrada terminou." << endl; 
    return 0; 
}

See working here .

    
29.05.2017 / 23:59