Reputation: 67
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!
Upvotes: 0
Views: 2174
Reputation: 2038
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...
Upvotes: 1
Reputation: 49986
The simplest solution is to use vector container:
std::vector<std::string> arr;
and then:
arr.push_back(line);
Upvotes: 2