Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') error when using #include <Windows.h> [duplicate]

Tags:

c++

max

cin

In Visual Studio 2010 Pro, I get a compile error stating "expected an identifier" on the max() portion of the command. It seems that in the windows.h header file there is a max(a,b) identifier and the compiler wants to use that.

I tried to use #include <limits> as well, but that did not fix the problem.

Is there anyway to get around this?

like image 786
jbolt Avatar asked Jun 27 '12 22:06

jbolt


People also ask

What is the significance of CIN ignore Numeric_limits Streamsize >:: max () '\ n ');?

'\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.

What is CIN Clear () in C++?

The cin. clear() clears the error flag on cin (so that future I/O operations will work correctly), and then cin. ignore(10000, '\n') skips to the next newline (to ignore anything else on the same line as the non-number so that it does not cause another parse failure).


1 Answers

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>.

like image 180
Ross Smith Avatar answered Sep 28 '22 16:09

Ross Smith