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
?
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
?
One way is to use istream::ignore
:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
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 use std::string
and the free function std::getline
:
std::string buffer;
getline(cin, buffer);
This will automatically read all the characters from stdin
up until a newline.
#import<string.h>
memset(buf, 0, 3);
This will fill the buffer with zeros.