String getline() problem
Mar 1, 2013 at 4:44pm Mar 1, 2013 at 4:44pm UTC
I've written the following code in order to get user input until the user writes a character previously entered:
1 2 3 4 5 6 7 8 9 10 11
string str, temp;
char c;
cout << "Insert the character that ends the input:" << endl;
cin >> c;
cout << "Insert the string:" << endl;
getline(cin, str, c);
Now, why doesn't the input end if I write the following?
Insert the character that ends the input:
}
Insert the string:
asdf
}
do it}
damn}
Mar 1, 2013 at 5:53pm Mar 1, 2013 at 5:53pm UTC
This is one of the hazards of mixing the extraction operator>> with getline() the extraction operator doesn't extract the end of line character so you will need to remove that character.
Topic archived. No new replies allowed.