mardi 2 février 2016

getline(cin, string) weird behavior c++

screenshot

For some reason I am forced to press enter twice to terminate my input and save it to the string. Why? What can I do about it?

Code:

cout << "Enter the message to encrypt: " << endl;
string openText;
getline(cin, openText);
clearBuffer();

Clear buffer:

void clearBuffer(){
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
}

EDIT: If I use cin instead of getline() it requires just one enter.

Aucun commentaire:

Enregistrer un commentaire