Question

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!

Was it helpful?

Solution

The simplest solution is to use vector container:

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

and then:

arr.push_back(line);

OTHER TIPS

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top