while loop not reading "\n"

how do i stop the loop?

1
2
3
4
5
6
7
8
9
10
  int main()
{
 string str;
 while(str != "\n")
 {
  cout<<"Insert data or a blank line to quit:\n";
  getline(cin,str);
  //run code that checks/reads data
 }
}
Last edited on
getline throws away the trailing line break... or leaves it in the buffer... I forget exactly. The point is, it doesn't get put in your string.

So if the user inputs an empty string... 'str' will be empty:

1
2
3
4
5
6
7
8
9
do
{
    cout<<"Insert data or a blank line to quit:\n";
    getline(cin,str);
    // ...
}while( !str.empty() );

// or
// }while( str != "" ); 
ah I see. Totally forgot about that .empty() function.. Thanks
Topic archived. No new replies allowed.