char buff[3];
cout<<"From: ";
cin.getline(buff, 3);
//something something
cout<<"To: ";
cin.getline(buff, 3);
How can I clear buffer at comment so extra chars don’t go to my second cin?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
One way is to use
istream::ignore:This will skip the maximum possible number of characters up until a newline is read.
For what it’s worth, though, you should probably not be using
istream::getline, as it works with raw C-style strings. A better option would be to usestd::stringand the free functionstd::getline:This will automatically read all the characters from
stdinup until a newline.