Loop goes on??

What i am trying to do is input until user inputs an empty line.
However, it doesn't seem to work.


#include <iostream>
#include <string>

using namespace std;

int main ()
{
string stri;
do
{
getline(cin, stri);
cin >> stri;
}
while (stri != "\n");

cout << stri << endl;
//return 0;
}
getline will not put the new line character in the string. Instead check if the stri is empty.
You don't need both getline() and >> as they both read the string. But you want to read the whole line so getline() is what you want.

I'd be tempted to do something like this:
1
2
3
4
5
6
std::string str;

while(std::getline(std::cin, str) && !str.empty())
{
    // ...
}
Thanks alot! That helped!
Topic archived. No new replies allowed.