I am using the following loop to read an unknown number of lines from the console, but it is not working. After I have fed the input I keeping pressing enter but the loop does not stop.
vector<string> file;
string line;
while(getline(cin,line)
file.push_back(line);
Because getline
will evaluate to true even if you push only enter.
You need to compare the read string
to the empty string and break if true.
vector<string> file;
string line;
while(getline(cin,line))
{
if (line.empty())
break;
file.push_back(line);
}
Try:
vector<string> file;
string line;
while( getline(cin,line))
{
if( line.empty())
break;
file.push_back(line);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With