Có ai rõ về lệnh cin, when and why do i need to use cin

I wrote a very basic program in C++ which asked the user lớn đầu vào a number & then a string. To my surprise, when running the program it never stopped khổng lồ ask for the string. It just skipped over it. After doing some reading on phukienotocaocap.com, I found out that I needed lớn add a line that said:

cin.ignore(256, " ");before the line that gets the string input đầu vào. Adding that fixed the problem & made the program work. My question is why does C++ need this cin.ignore() line & how can I predict when I will need lớn use cin.ignore()?

Here is the program I wrote:

#include #include using namespace std;int main(){ double num; string mystr; cout > num; cout
c++ cin getline ignore
Improve this question
edited Aug 24 "14 at 19:26


6,47999 gold badges3737 silver badges6565 bronze badges
asked Aug 24 "14 at 19:17

93411 gold badge88 silver badges99 bronze badges
Add a bình luận |

7 Answers 7

Active sầu Oldest Votes
Ignore is exactly what the name implies.

Bạn đang xem: Có ai rõ về lệnh cin, when and why do i need to use cin

It doesn"t "throw away" something you don"t need instead, it ignores the amount of characters you specify when you điện thoại tư vấn it, up to the char you specify as a breakpoint.

It works with both input và output buffers.

Essentially, for std::cin statements you use ignore before you vì a getline hotline, because when a user inputs something with std::cin, they hit enter and a " " char gets inlớn the cin buffer. Then if you use getline, it gets the newline char instead of the string you want. So you bởi vì a std::cin.ignore(1000," ") and that should clear the buffer up to lớn the string that you want. (The 1000 is put there to skip over a specific amount of chars before the specified break point, in this case, the newline character.)

Improve this answer
edited Jan 30 "16 at 22:53

Michael Hoffmann
2,25222 gold badges20trăng tròn silver badges3939 bronze badges
answered Aug 24 "14 at 20:46

96766 silver badges88 bronze badges
Add a phản hồi |
You"re thinking about this the wrong way. You"re thinking in logical steps each time cin or getline is used. Ex. First ask for a number, then ask for a name. That is the wrong way to lớn think about cin. So you run into a race condition because you assume the stream is clear each time you ask for a input đầu vào.

If you write your program purely for input you"ll find the problem:

void main(void){ double num; string mystr; cin >> num; getline(cin, mystr); cout In the above sầu, you are thinking, "first get a number." So you type in 123 press enter, và your output will be num=123,mystr="". Why is that? It"s because in the stream you have 123 & the 123 is parsed inkhổng lồ the num variable while is still in the stream. Reading the doc for getline function by default it will look in the istream until a is encountered. In this example, since is in the stream, it looks like it "skipped" it but it worked properly.

Xem thêm: “ Giải Khuyến Khích Tiếng Anh Là Gì ? Giải Khuyến Khích Tiếng Anh Là Gì

For the above sầu to lớn work, you"ll have lớn enter 123Hello World which will properly output num=123,mystr="Hello World". That, or you put a cin.ignore between the cin & getline so that it"ll break into lớn logical steps that you expect.

This is why you need the ignore command. Because you are thinking of it in logical steps rather than in a stream khung so you run inkhổng lồ a race condition.

Take another code example that is commonly found in schools:

void main(void){ int age; string firstName; string lastName; cout > firstName; cout > lastName; cout > age; cout The above seems to lớn be in logical steps. First ask for first name, last name, then age. So if you did John enter, then Doe enter, then 19 enter, the application works each ngắn gọn xúc tích step. If you think of it in "streams" you can simply enter John Doe 19 on the "First name:" question and it would work as well & appear lớn skip the remaining questions. For the above sầu khổng lồ work in logical steps, you would need khổng lồ ignore the remaining stream for each logical break in questions.

Just rethành viên khổng lồ think of your program input as it is reading from a "stream" & not in logical steps. Each time you hotline cin it is being read from a stream. This creates a rather buggy application if the user enters the wrong đầu vào. For example, if you entered a character where a cin >> double is expected, the application will produce a seemingly bizarre output.