C++ cin.ignore(numeric_limits<streamsize>::max(), '\n'); max() 无法识别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20446373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cin.ignore(numeric_limits<streamsize>::max(), '\n'); max() not recognize it
提问by user3078454
I'm taking an intro to C++, and I'm using VStudio 2013 on Win7. I try to avoid the wrong data input from my menus, and it's working in all of them except this one.
我正在介绍 C++,我在 Win7 上使用 VStudio 2013。我尽量避免从我的菜单中输入错误的数据,它在所有菜单中都有效,除了这个。
cout << "Please choose your second number" << endl;
cin >> move2;
if (move2 < 1 || move2 > size)
{
cout << "That's not a valid move" << endl;
Sleep(2000);
cin.sync();
cin.clear();
}
the only difference is that in the condition for move > a variable (size) not a number. When I enter a char it goes back to the question asking for another input, but if I enter a word, it breaks!
唯一的区别是在 move > 一个变量(大小)而不是一个数字的条件下。当我输入一个字符时,它会回到要求另一个输入的问题,但是如果我输入一个单词,它就会中断!
I try to use cin.ignore(numeric_limits<streamsize>::max(), '\n');
but the compiler highlights max()
and it says "expecting identifier".
我尝试使用cin.ignore(numeric_limits<streamsize>::max(), '\n');
但编译器突出显示max()
并显示“期望标识符”。
It maybe easy for all of you good programmers, but I don't know how to fix it. Can anybody help me?
对于你们所有优秀的程序员来说,这可能很容易,但我不知道如何解决。有谁能够帮助我?
回答by loop
This is because in Visual Studio when you use the windows includes it will define a macro max(). If you hover over the max() in your example you should receive an intellisense warning. You can solve this problem by undefining max after all your includes and before any code.
这是因为在 Visual Studio 中,当您使用 windows 包含时,它将定义一个宏 max()。如果您将鼠标悬停在示例中的 max() 上,您应该会收到智能感知警告。您可以通过在所有包含之后和任何代码之前取消定义 max 来解决此问题。
#undef max
回答by Dietmar Kühl
To use std::numeric_limits<T>
you'll need to include <limits>
. Further, the type passed to it need to be known andactually the type std::streamsize
, i.e., I would use it as
要使用,std::numeric_limits<T>
您需要包含<limits>
. 此外,传递给它的类型需要知道,实际上是 type std::streamsize
,即,我会将它用作
#include <iostream>
#include <limits>
// ...
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Also, you should probably make sure that your attempt to read something was actually successful and, if it was not, first clear()
the stream's state. Here is a complete program (it certainly compiles and runs with gccand clang):
此外,您可能应该确保您尝试读取某些内容实际上是成功的,如果不是,请首先clear()
检查流的状态。这是一个完整的程序(它当然可以用gcc和clang编译和运行):
#include <iostream>
#include <limits>
int main()
{
int move2, size(3);
while (!(std::cin >> move2) || move2 < 1 || size < move2) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "invalid input ignored; please enter a valid move\n";
}
std::cout << "move2=" << move2 << '\n';
}
回答by Xitalogy
Code that Limits Potential Side Effects
限制潜在副作用的代码
In my testing, #undef max
does not seem to need to be done before any #include
s.
在我的测试中,#undef max
似乎不需要在任何#include
s之前完成。
You might want to limit potential side effects in general by first pushing the max
macro, then undefining it, then popping it off after using max from <limits.h>
like this (only tested on Windows/Visual Studio 2019):
您可能希望通过首先推送max
宏,然后取消定义它,然后<limits.h>
像这样使用 max from 后弹出它来限制潜在的副作用(仅在 Windows/Visual Studio 2019 上测试):
// stuff ...
#pragma push_macro("max")
#undef max
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
#pragma pop_macro("max")
// more stuff ...
See the Microsoft Docs: push_macroand pop_macro
请参阅 Microsoft Docs:push_macro和pop_macro
Put it in a function to make it DRY:
把它放在一个函数中以使其干燥:
void CinIgnore() {
#pragma push_macro("max")
#undef max
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
#pragma pop_macro("max")
}
Add std::cin.clear
to make a CinReset
function:
添加std::cin.clear
一个CinReset
函数:
void CinReset() {
std::cin.clear();
#pragma push_macro("max")
#undef max
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
#pragma pop_macro("max")
}
Additional information: this cppreference.com articleon ignore
has a good example of how to use clear
and ignore
, along with state flags, effectively, but does not have the necessary fix for Windows/Visual Studio noted above.
其他信息:此cppreference.com文章就ignore
对如何使用一个很好的例子clear
,并ignore
与状态标志,有效沿,但没有针对Windows进行必要的修复/ Visual Studio中如上所述。
NOTE: The above, though standard, does not work well with the user interactively inputting EOF (ctrl-z
- Windows using Visual Studio). I have written a IStreamUtils
class that includes a Reset method that is more robust. I will add a link to an article about that here later.
注意:上述内容虽然是标准的,但在用户交互式输入 EOF(ctrl-z
- 使用 Visual Studio 的 Windows)时效果不佳。我编写了一个IStreamUtils
包含更强大的 Reset 方法的类。稍后我将在此处添加有关该文章的链接。
回答by chasep255
If cin has an error the following will clear the error state and then read in the rest of the line.
如果 cin 有错误,以下内容将清除错误状态,然后读取该行的其余部分。
if(!cin)
{
cin.clear();
string junk;
getline(cin, junk);
}
回答by Katie Stevers
Include:
包括:
#include <climits>
and see if that helps. I had the same issue and when I changed it from limits to climits it was resolved.
看看这是否有帮助。我遇到了同样的问题,当我将其从限制更改为 climit 时,它解决了。