1
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?

templatetypedef
  • 345,949
  • 98
  • 857
  • 1,030
Thomas B
  • 1,129
  • 2
  • 8
  • 5

3 Answers3

5

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.

templatetypedef
  • 345,949
  • 98
  • 857
  • 1,030
0

Take a look at istream::ignore

Ed S.
  • 119,398
  • 20
  • 176
  • 254
0
#import<string.h>

memset(buf, 0, 3);

This will fill the buffer with zeros.

DarthJDG
  • 16,331
  • 11
  • 48
  • 55
Maciek
  • 3,014
  • 1
  • 20
  • 26