0
#include <iostream>
#include <istream>
using namespace std;
int main()
{
    char a = '\0';
    char b = '\0';
    cin.get(a);
    cout << a <<endl;
    cin.sync();
    cin.get(b);
    cout << b <<endl;
    return 0;
}

input:

sssssssssssssssss\n

result:

s
s

The correct result I expect is I can type twice,even though I type many characters the first time! like this:

input:

ssssssssssssssss\n

the first cout:

s

wait my second typing:

wwwwwwwwwwwww\n

the second cout:

w

I want to use cin.sync() to clear input buffers, but why it does not work?

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
Ben-xue
  • 103
  • 1
  • 6
  • 3
    Start by reading what [`std:::istream::sync`](http://en.cppreference.com/w/cpp/io/basic_istream/sync) actually *does*, because I doubt it does what you seem to think, and as a consequence, that it will serve you're intended purpose to "clear input buffers". If you want to sweep an input stream through an impending newline, [there are ways to do that](https://stackoverflow.com/a/257182/1322972). – WhozCraig Aug 14 '17 at 03:17
  • `sync()` only clears input with MSVC. Use `ignore()` instead: https://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input#comment13661446_10553849 – Ry- Aug 14 '17 at 03:21

0 Answers0