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