How do I flush the cin buffer?
Possibly:
std::cin.ignore(INT_MAX);
This would read in and ignore everything until EOF
. (you can also supply a second argument which is the character to read until (ex: '\n'
to ignore a single line).
Also: You probably want to do a: std::cin.clear();
before this too to reset the stream state.
Proper way to flush cin input
Answered by mikedu95 in a comment:
Read a whole line with
getline
, then perform string to integer conversion usingstoi
#include <string>
#include <iostream>
using namespace std;
string str;
int a, b, c, d;
void main()
{
cout << "Reading a" << endl;
getline(cin, str);
a = stoi(str);
cout << "Reading b" << endl;
getline(cin, str);
b = stoi(str);
cout << "Reading c" << endl;
getline(cin, str);
c = stoi(str);
cout << a << " " << b << " " << c << endl;
system("pause");
}
How to clear cin buffer after reading char
You asked:
Is there any function to reset or clear buffer of cin.
There is std::istream::ignore()
. There is example code at the given link which shows how to use the function.
In your case, I see something like:
int i;
int c;
std::cin >> c;
std::cin >> i;
if (std::cin.bad()) {
std::cin.clear(); // unset failbit
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // skip bad input
std::cin >> i;
}
Clear the cin buffer before another input request, c++
You might use ignore to drop the newline from the buffer (e.g. drop X characters before the newline as delimiter). Extraction and ignore stop when the delimiter is extracted.
e.g.
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Also related: Why would we call cin.clear() and cin.ignore() after reading input?
How to prevent `std::cin` or `\n` from flushing the buffer of `std::cout`?
You could write to your own std::stringstream
buffer, and then output that to std::cout
when you're ready.
MWE:
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <vector>
using std::cin;
using std::cout;
using std::istream;
using std::runtime_error;
using std::stringstream;
using std::vector;
static auto get_int(istream& in) -> int {
int n;
if (!(in >> n)) {
throw runtime_error("bad input");
}
return n;
}
int main() {
auto ss = stringstream();
auto v = vector<int>();
auto size = get_int(cin);
while(size--) {
auto n1 = get_int(cin);
if (n1 == 0) {
auto n2 = get_int(cin);
v.push_back(n2);
} else if (n1 == 1) {
auto n2 = get_int(cin);
ss << v[n2] << '\n';
} else if (n1 == 2) {
v.pop_back();
}
}
cout << ss.str();
}
Related Topics
How to Print a List of Elements Separated by Commas
Opencv C++/Obj-C: Detecting a Sheet of Paper/Square Detection
When to Use References Vs. Pointers
Reflection and Refraction Impossible Without Recursive Ray Tracing
How to Concatenate a Std::String and an Int
Can a Class Member Function Template Be Virtual
Generating Random Integer from a Range
Difference Between 'Typedef' and 'Using' in C++11
Delete VS Delete[] Operators in C++
What Exactly Is One Definition Rule in C++
Generate Random Numbers Uniformly Over an Entire Range
What Is the Lifetime of a Static Variable in a C++ Function
Is Local Static Variable Initialization Thread-Safe in C++11
Using Base Pointer Register in C++ Inline Asm
Difference Between Public, Private, and Protected Inheritance in C++