// Description: Illustrate the use of cin to get input// and how to recover from errors.#include using namespace std;int main(){ int input_var = 0; // Enter the do while loop and stay there until either // a non-numeric is entered, or -1 is entered.

Bạn đang xem: Cin.ignore là gì

Note that // cin will accept any integer, 4, 40, 400, etc. do { cout > input_var)) { cout What is the significance of cin.clear() and cin.ignore()? Why are the 10000 and parameters necessary?

c++ input iostream cin Share Improve this question Follow edited Apr 3 “20 at 12:24


flightflight 6,75344 gold badges2222 silver badges3030 bronze badges 1 Add a comment | 28Why do we use:

1) cin.ignore

2) cin.clear



1) To ignore (extract and discard) values that we don”t want on the stream

2) To clear the internal state of stream. After using cin.clear internal state is set again back to goodbit, which means that there are no “errors”.

Long version:

If something is put on “stream” (cin) then it must be taken from there.

Xem thêm: Cách Nấu Măng Khô Với Xương Của Thu__Trang, 4 Cách Nấu Canh Măng Khô Ngọt Lịm

By “taken” we mean “used”, “removed”, “extracted” from stream. Stream has a flow. The data is flowing on cin like water on stream. You simply cannot stop the flow of water