vendredi 24 février 2017

How to read console input and store it into a vector string

I'm trying to make a program that takes input from the console, and reads each word and number separated by spaces ans stores it in a vector string for further processing.

void foo() {
    string input;

    vector<string> list;




    while (getline(cin, input, ' '))
    {
        list.push_back(input);
    }

    for (int counter = 0; counter < list.size(); counter++) 
    {
        cout << list[counter] << endl;
    }

};




int main()
{

    foo();

    return 0;
}

The problems comes when getline doesn't stop reading at newlines and you need to ctrl+z to EOF. Is there anyway to make getline stop at newline?

Aucun commentaire:

Enregistrer un commentaire