Cin.ignore là gì

  -  

// 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 vày while loop & stay there until either // a non-numeric is entered, or -1 is entered. Lưu ý that // cin will accept any integer, 4, 40, 400, etc. Bởi { cout > input_var)) { cout What is the significance of cin.clear() & cin.ignore()? Why are the 10000 and parameters necessary?

c++ input iostream cin cốt truyện Improve this question Follow edited Apr 3 “20 at 12:24




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

*

Ardent Coder 3,26999 gold badges1818 silver badges3939 bronze badges asked Feb 27 “11 at 5:17


*

JacKeownJacKeown 2,09055 gold badges2424 silver badges2929 bronze badges 1 add a phản hồi |

4 Answers 4

Active Oldest Votes105The cin.clear() clears the error flag on cin (so that future I/O operations will work correctly), và then cin.ignore(10000, ”“) skips khổng lồ the next newline (to ignore anything else on the same line as the non-number so that it does not cause another parse failure). It will only skip up to lớn 10000 characters, so the code is assuming the user will not put in a very long, invalid line.Bạn vẫn xem: Cin.ignore là gì

Đang xem: Cin.ignore là gì

nội dung Improve this answer Follow answered Feb 27 “11 at 5:36


*



Xem thêm: "Đồ Chơi Thời Trang" Giá Tốt Tháng 8, 2021 Búp Bê, Game Thời Trang Em Bé

Jeremiah WillcockJeremiah Willcock 27.1k55 gold badges6767 silver badges7474 bronze badges 6 31 If you want khổng lồ use std::numeric_limits, make sure khổng lồ #include | Show 1 more comments 47You enter the

if (!(cin >> input_var))statement if an error occurs when taking the input from cin. If an error occurs then an error flag is set & future attempts to get input will fail. That”s why you need

mô tả Improve this answer Follow answered Feb 27 “11 at 5:44


*

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

1) cin.ignore

2) cin.clear

?

Simply:

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

2) to lớn clear the internal state of stream. After using cin.clear internal state is phối again back khổng lồ goodbit, which means that there are no “errors”.

Long version:

If something is put on “stream” (cin) then it must be taken from there. 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

Follow Us


Có gì mới




Xem thêm: Nạp Tiền Steam Bằng Thẻ Điện Thoại Ít Ai Biết, Phí Cao Không

Trending