46

What does cin.ignore(numeric_limits<streamsize>::max(), '\n') mean in C++?

Does it actually ignore the last input from the user?

kaya3
  • 47,440
  • 4
  • 68
  • 97
Zyi
  • 473
  • 1
  • 5
  • 4

3 Answers3

47

This line ignores the rest of the current line, up to '\n' or EOF - whichever comes first:

  • '\n' sets the delimiter, i.e. the character after which cin stops ignoring
  • numeric_limits<streamsize>::max() sets the maximum number of characters to ignore. Since this is the upper limit on the size of a stream, you are effectively telling cin that there is no limit to the number of characters to ignore.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    So the max() determine how many characters to ignore from the cin input, is that right? In my case, it's everything since it is max? @dasblinkenlight – Zyi Jul 29 '14 at 18:48
  • @Zyi Yes, the first parameter determines how many characters to ignore. In your case that's `max()`, meaning "ignore as much as is needed." – Sergey Kalinichenko Jul 29 '14 at 19:00
  • as there might be something like this: typedef _Longlong streamsize; it have to assume that max() will return a value of maybe LONGLONG_MAX - this is a finite number. no reason to assume "there is no limit". the limit is thus a finite value but at the same time a rather big value. – Alexander Stohr Jun 26 '19 at 14:47
  • it extracts and discards the rest of the current line, why are so many answers saying it "ignores" – csguy Nov 05 '19 at 05:41
  • 4
    @csguy Because "extract and discard" is a low-level description of what it means for a program to "ignore" characters. – Sergey Kalinichenko Nov 05 '19 at 12:50
0

cin.ignore(numeric_limits < streamsize > ::max(), '\n');

Here, the \n acts as a delimiter.... that is the point upto which the code has to be ignored(as "\n" in this perticular case). And max() defines that there is no limit for how much can be ignored, the spaces, tabs have to be ignored untill the line ends.

Akash sharma
  • 489
  • 4
  • 5
0

This code says that the input has to be ignored on 2 basis -1 if '\n' is read -2 if limit is reached

as limit is max therefore only '\n' is the main condition to be considered