我刚刚完成了这个大理石游戏程序的工作,它工作得很好,除了我需要做一件事来保护它免受无效输入。程序正确响应所有基于整数的无效输入,但如果输入非整数,则程序崩溃。我一直在使用cin.fail()函数,它起作用,但随后陷入循环。如果用户输入一个非整数,我希望程序告诉用户这是无效的输入,并提示用户再次输入他们的选择。
我添加了return 1;在无效的输入语句之后结束程序,而不是它崩溃,但这不是我想要做的。我希望它打印"无效输入",然后重新提示用户再次输入他们的选择。
#include <iostream>
using namespace std;
int main()
{
cout << "*** The Game of Nim ***nn" << endl;
string player1, player2, currentPlayer;
int marbles, selection;
bool isDone = false;
cout << "Enter a name for Player 1: ";
cin >> player1;
cout << "Enter a name for Player 2: ";
cin >> player2;
cout << "Enter how many marbles you would like to start with in the pile: ";
cin >> marbles;
cout << "nThere are " << marbles << " marbles in the pilen" << endl;
currentPlayer = player1;
do
{
bool turnDone = false;
while (turnDone == false && marbles != 0)
{
if (currentPlayer == player1)
{
cout << currentPlayer << "n";
cout << "How many marbles would you like to draw? : ";
cin >> selection;
if (cin.fail())
{
cout << "nInvalid Inputn";
return 1;
}
if (selection < marbles / 2 + 1 && marbles > 1 && selection != 0)
{
marbles = marbles - selection;
if (marbles == 1)
{
cout << "nUh Oh! There is only 1 marble remaining in the pilen" << endl;
currentPlayer = player2;
turnDone = true;
break;
}
cout << "nThere are " << marbles << " marbles remaining in the pilen" << endl;
currentPlayer = player2;
turnDone = true;
}else if (marbles == 1)
{
marbles = marbles - selection;
if (marbles <= 0)
{
cout << "nThere are 0 marbles remianing in the pilen";
cout << "You drew the last marble!" << endl;
cout << "You lose!nn" << endl;
cout << player2 << " winsnn" << endl;
turnDone = true;
return 0;
}
}else if (selection == 0 || selection > marbles / 2 + 1)
{
cout << "nYou must draw at least 1 marble, and no more than half the pilen" << endl;
}
}else if (currentPlayer == player2)
{
cout << currentPlayer << "n";
cout << "How many marbles would you like to draw? : ";
cin >> selection;
if (selection < marbles / 2 +1 && marbles > 1 && selection != 0)
{
marbles = marbles - selection;
if (marbles == 1)
{
cout << "nUh Oh! There is only 1 marble remaining in the pilen" << endl;
currentPlayer = player1;
turnDone = true;
break;
}
cout << "nThere are " << marbles << " marbles remaining in the pilen" << endl;
currentPlayer = player1;
turnDone = true;
}else if (marbles == 1)
{
marbles = marbles - selection;
if (marbles <= 0)
{
cout << "nThere are 0 marbles remianing in the pilen";
cout << "You drew the last marble!" << endl;
cout << "You lose!nn" << endl;
cout << player1 << " wins!nn" << endl;
turnDone = true;
return 0;
}
}else if (selection == 0 || selection > marbles / 2 + 1)
{
cout << "nYou must draw at least 1 marble, and no more than half the pilen" << endl;
}
}
}
}while (isDone == false);
return 0;
}
下面是如何在程序中使用std::cin.fail():
int marbles;
bool check = false;
do {
std::cout << "Enter how many marbles you would like to start with in the pile: ";
std::cin >> marbles;
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore();
}
else
check = true;
}while(!check);
cin.fail()将返回一个布尔值,表示是否收到了预期的收入
如果没有,则必须使用clear(),它将失败值设置为false,然后忽略给定的输入
希望对你有所帮助
获取用户输入的字符串并检查其中是否有数字:
std::string input;
std::cin >> input;
int marbles = std::atoi(input.c_str()); //Returns the first number met in the char table, if there is none, returns 0
if(marbles)
std::cout << marbles;