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
  • 362,284
  • 104
  • 897
  • 1,065
Thomas B
  • 1,289
  • 2
  • 9
  • 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
  • 362,284
  • 104
  • 897
  • 1,065
0

Take a look at istream::ignore

Ed S.
  • 122,712
  • 22
  • 185
  • 265
0
#import<string.h>

memset(buf, 0, 3);

This will fill the buffer with zeros.

DarthJDG
  • 16,511
  • 11
  • 49
  • 56
Maciek
  • 3,174
  • 1
  • 22
  • 26