cin.ignore(numeric_limits streamsize ::max(), '\n')
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 whichcin
stops ignoringnumeric_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 tellingcin
that there is no limit to the number of characters to ignore.
cin.ignore(std::numeric_limits std::streamsize ::max(), '\n'); Gives me an error
You have to add
#include <limits>
to use std::numeric_limits
, so add that to the top of your code.
std::cin.ignore(std::numeric_limits std::streamsize ::max(), '\n') error when using #include Windows.h
The <windows.h>
header has had the min()
and max()
macros since time immemorial, and they frequently cause problems with C++. Fortunately, you can disable them by adding #define NOMINMAX
before including <windows.h>
.
Value of numeric_limits streamsize ::max() in c++
isn't this a very large value exceeding my HDD space.
That's exactly the purpose of this value. You want to skip as many char as possible. In fact, this value does indicate infinite, since the count test is disabled for this value:
count characters were extracted. This test is disabled in the special
case when count equals std::numeric_limits< std::streamsize >::max()
Does cin.ignore with a limit continue discarding new input?
Your code is clearing any error flag and then discarding additional characters until the hard limit of numeric_limits<streamsize>::max()
or an EOF
character (you can usually enter one with Ctrl+Z) is hit. Therefore it is, as you suspected, continuously discarding your input.
A fixed version of your code could just discard characters until a newline is found
#include <iostream>
#include <limits>
using namespace std;
int main()
{
int num;
// Get valid input
do
{
if (cin.fail())
{
// Clear error state and flush any garbage input
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Please enter a number: ";
cin >> num;
} while (cin.fail());
// Echo back what we heard
cout << "You entered: " << num << "\n";
cin.get();
}
Live Example
And possibly a more idiomatic and compact version
#include <iostream>
#include <limits>
using namespace std;
int main()
{
int num;
// Get valid input
while( (cout << "Please enter a number: ") && !(cin >> num) )
{
cout << "Invalid number entered" << endl;
// Clear error state and flush any garbage input
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());
}
// Echo back what we heard
cout << "You entered: " << num << "\n";
cin.get();
}
Live Example
As you noted cin.rdbuf()->in_avail()
might be an easier solution since it will discard exactly the number of characters available to read in the stream.
cin ws vs cin.ignore(numeric_limits streamsize ::max(), '\n')?
First of all, ignore
discards all characters until the delimiter, not only white-space.
Second of all, ignore
discards until the specified delimiter character, which could be any character (not only newline) while ws
ignore leading space, until there's any non-space character.
Third of all, newline '\n'
is a white-space character so would be discarded by the ws
manipulator.
Related Topics
What Would Be C++ Limitations Compared C Language
How to Write C++ Getters and Setters
Memory Allocation Char* and Char[]
Do All Pointers Have the Same Size in C++
How to Set the Baud Rate to 307,200 on Linux
Get Hwnd on Windows with Qt5 (From Wid)
Trial-Division Code Runs 2X Faster as 32-Bit on Windows Than 64-Bit on Linux
How to Use _Crt_Secure_No_Warnings
Default Argument in the Middle of Parameter List