In my knowledge when using getline()
after cin
, we need to flush the newline character in the buffer first, before calling getline()
,
signed main(){
/*
* input is of next two lines
* Stack Overflow
* Where developers learn.
* for more about cin.ignore visit http://www.cplusplus.com/reference/istream/istream/ignore/
*/
string name, address;
cin>>name;
//cin.ignore();
//cin.ignore(256,'\n');
getline(cin,address);
cout << address << endl;
assert(address == " Overflow"); // breaks if using cin.ignore() or cin.ignore(256,'\n')
assert(address == "Overflow"); // breaks if using cin.ignore(256,'\n') OR not using any
assert(address == "Where developers learn."); // breaks if using cin.ignore() or not using ignore
}
enter the input
Stack Overflow
Where developers learn.