Istream::ignore

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

cin.ignore(256, " ");before the line that gets the string input. Adding that fixed the problem & made the program work. My question is why does C++ need this cin.ignore() line và how can I predict when I will need to 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
cốt truyện
Improve this question
Follow
edited Aug 10, 2021 at 23:31

*

kaya3
40.7k44 gold badges5050 silver badges7878 bronze badges
asked Aug 24, 2014 at 19:17
*

RaddicusRaddicus
1,00411 gold badge99 silver badges99 bronze badges
5
showroom a phản hồi |

7 Answers 7


Sorted by: Reset to default
Highest score (default) Date modified (newest first) Date created (oldest first)
82
Ignore is exactly what the name implies.

Bạn đang xem: Istream::ignore

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 khổng lồ the char you specify as a breakpoint.

It works with both input & output buffers.

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


cốt truyện
Improve this answer
Follow
edited Jan 30, năm 2016 at 22:53
*

Michael Hoffmann
2,33122 gold badges2222 silver badges4141 bronze badges
answered Aug 24, 2014 at 20:46
*

savageWayssavageWays
1,00777 silver badges88 bronze badges
4
add a comment |
42
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 khổng lồ think about cin. So you run into a race condition because you assume the stream is clear each time you ask for a input.

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, you are thinking, "first get a number." So you type in 123 press enter, và your output đầu ra will be num=123,mystr="". Why is that? It"s because in the stream you have 123 và the 123 is parsed into 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: Miếng Dán Chống Chói Màn Hình Máy Tính, Màn Hình Chống Chói Anti Glare Là Gì

For the above khổng lồ work, you"ll have lớn enter 123Hello World which will properly đầu ra num=123,mystr="Hello World". That, or you put a cin.ignore between the cin và getline so that it"ll break into 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 size so you run into 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 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 và it would work as well & appear khổng lồ skip the remaining questions. For the above to work in logical steps, you would need to lớn ignore the remaining stream for each logical break in questions.

Xem thêm: Cắt Tròng Kính Cận Bao Nhiêu Tiền ? Ở Đâu Tốt? I Đa Khoa Phương Nam

Just remember to lớn think of your program input đầu vào 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 input. For example, if you entered a character where a cin >> double is expected, the application will produce a seemingly bizarre output.