使用std :: cin检查有效的类型输入(C ++) [英] Checking for valid type input using std::cin (C++)
问题描述
我正在为C ++类开发游戏 Hex,但在理解如何确保cin读取正确类型方面遇到困难。我已经进行了一些研究,但我接近了,但这是我的问题:
I am working on the game 'Hex' for a C++ class, and am having trouble understanding how to make sure cin is reading the correct type. I have done some research and I'm close, but here is my problem:
int i, j;
while(true){
cin >> i;
if(cin.fail() ){ //if type wasn't right
cin.clear(); //clear stream
cin.ignore(); //ignore left over data
cout << "First value is invalid input! Valid coordinates are in range 0 to " << size - 1 << endl;
continue;
}
cin >> j;
if(cin.fail() ){ //if type wasn't right
cin.clear(); //clear stream
cin.ignore(); //ignore left over data
cout << "Second value is invalid input! Valid coordinates are in range 0 to " << size - 1 << endl;
continue;
}
if(i < 0 or i >= size or j < 0 or j >= size){
//prompt that input is wrong
cout << "Invalid input! Valid coordinates are in range 0 to " << size - 1 << endl;
continue;
}
那里有break语句,确实可以。我的问题是这样的运行:
There are break statements there, it does get out okay. My problem is with runs such as this:
Player 1's turn
Enter the row and column coordinate with only a space inbetween
a b
First value is invalid input! Valid coordinates are in range 0 to 10
First value is invalid input! Valid coordinates are in range 0 to 10
u 8
First value is invalid input! Valid coordinates are in range 0 to 10
8 u
当第二个值都不正确时,并且第二个值都不正确时,它不会将其检测为错误。我看过 http://www.cplusplus.com/reference/iostream/ cin /?kw = cin ,它甚至没有有关故障,清除和忽略功能的文档,因此我并不完全了解它们。
It doesn't detect that the second value was incorrect when they both were, and when only the second is incorrect, it doesn't detect it as an error. I have looked at http://www.cplusplus.com/reference/iostream/cin/?kw=cin and it doesn't even have the documentation for the fail, clear, and ignore functions, so I don't fully understand them. Thanks in advance!
推荐答案
u 8
First value is invalid input! Valid coordinates are in range 0 to 10
8 u
为 cin>> i;
由于 u
而失败,重复循环,但仅 cin $之后的第一个字符c $ c>流(一个空间)由
cin.ignore();
占用。随着循环再次开始, cin>> i;
是使用流中剩余的内容执行的,在本例中为 8
。这意味着在错误消息后,第一个值无效输入!有效坐标的范围是0到10
,程序实际上正在等待 cin>> j;
。输入 8 u
时,它将首先读取 8
并将其值分配给j,这意味着没有失败。循环结束,流中剩下 u
。
As cin >> i;
fails because of u
, the loop is repeated, but only the first character following in the cin
stream, a space, is consumed by cin.ignore();
. As the loop starts again, cin >> i;
is executed using what's left in the stream, in this case 8
. This means that after the error message First value is invalid input! Valid coordinates are in range 0 to 10
, the program is actually waiting on cin >> j;
. When 8 u
is entered, it reads 8
first and assigns its value to j, meaning there is no failure. The loop ends, and u
is left in the stream.
要解决此问题,请更改 cin.ignore();
通过 cin.ignore(numeric_limits< streamsize> :: max(),'\n');
。除了忽略下一个字符(在此示例中为空格)之外,它还将忽略行的其余部分,从而有效地重置 cin
流。您需要包含< limits>
标头。
To fix this, change cin.ignore();
by cin.ignore(numeric_limits<streamsize>::max(), '\n');
. Instead of ignoring only the next character, which is a space in this example, it'll ignore the rest of the line, effectively resetting the cin
stream. You'll need to include the <limits>
header.
这篇关于使用std :: cin检查有效的类型输入(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!