Вопрос

I'm using getline to get input and I want to store every line input to an array so that I can recall specific array content for further processing. Any ideas? Thanks in advance

string line;

while (true) {

    getline(cin, line);

    if (line.empty()) {
        break;
    }
    // code

}

EDIT/ADD

Does anyone know why I cannot use cin before the while loop? When for example I put cin >> var1; before the loop it won't go inside the loop at all!

ANSWERING MYSELF

I found this that solves it!

Это было полезно?

Решение

The simplest solution is to use vector container:

 std::vector<std::string> arr;

and then:

arr.push_back(line);

Другие советы

Use vector<string> vec;

Better way to read from file would be as below

string line;

while (getline(cin, line)) {

if (line.empty()) {
    break;
}
vec.push_back(line);
// code

}

once EOF is reached loop will break...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top