CIN

     

// Description: Illustrate the use of cin to lớn get input// và how khổng lồ recover from errors.#include using namespace std;int main(){ int input_var = 0; // Enter the vị 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. Do { cout > input_var)) Show 1 more comment
56
You 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 và future attempts to get input will fail. That"s why you need

cin.clear();to get rid of the error flag. Also, the input đầu vào which failed will be sitting in what I assume is some sort of buffer. When you try lớn get đầu vào again, it will read the same input đầu vào in the buffer and it will fail again. That"s why you need

cin.ignore(10000," ");It takes out 10000 characters from the buffer but stops if it encounters a newline ( ). The 10000 is just a generic large value.

Bạn đang xem: Cin


nội dung
Improve this answer
Follow
answered Feb 27, 2011 at 5:44
*

flightflight
7,13944 gold badges2323 silver badges3131 bronze badges
1
địa chỉ cửa hàng a comment Of course this can be improved by for example doing what you did in question using loop while.

BONUS:

You might be wondering. What about if I wanted to lớn get name & surname in the same line from the user? Is it even possible using cin if cin interprets each value separated by "space" as different variable?

Sure, you can vì chưng it two ways:

1)

string name, surname;cout > name;cin >> surname;cout 2) or by using getline function.

getline(cin, nameOfStringVariable);and that"s how to vị it:

string nameAndSurname;cout The second option might backfire you in case you use it after you use "cin" before the getline.

Let"s check it out:

a)

int age;cout > age;cout If you put "20" as age you won"t be asked for nameAndSurname.

But if you vì it that way:

b)

string nameAndSurname;cout > age;cout everything is fine.

Xem thêm: Đốt Cháy Este Đơn Chức, Mạch Hở Ta Luôn Có:, Phương Pháp Giải Bài Tập Đốt Cháy Este

WHAT?!

Every time you put something on input (stream) you leave at the kết thúc white character which is ENTER (" ") You have to somehow enter values lớn console. So it must happen if the data comes from user.

b) cin characteristics is that it ignores whitespace, so when you are reading in information from cin, the newline character " " doesn"t matter. It gets ignored.

a) getline function gets the entire line up to lớn the newline character (" "), & when the newline char is the first thing the getline function gets " ", and that"s all khổng lồ get. You extract newline character that was left on stream by user who put "20" on stream in line 3.

So in order to lớn fix it is to always invoke cin.ignore(); each time you use cin to get any value if you are ever going lớn use getline() inside your program.

Xem thêm: Top 25 Bài Tả Một Cây Non Mới Trồng 2022 Siêu Hay, Tả Một Cây Non Mới Trồng Hay Chọn Lọc

So the proper code would be:

int age;cout > age;cin.ignore(); // it ignores just enter without arguments being sent. It"s same as cin.ignore(1, " ") cout I hope streams are more clear to lớn you know.