问题描述
//checks every turn whether player 1 or player 2 wins using switch and case method
void caseCheck()
{
//when a player wins
if (x == "O" && y == "O" && z == "O" || a == "O" && b == "O" && c == "O" || d == "O" && e == "O" && f == "O" || x == "O" && a == "O" && d == "O" || y == "O" && b == "O" && e == "O" || z == "O" && c == "O" && f == "O" || x == "O" && b == "O" && f == "O" || z == "O" && b == "O" && d == "O")
{
system("cls");
mainBoard();
cout << endl << "player 2 wins";
throw; // here is where the error is
}
if (x == "X" && y == "X" && z == "X" || a == "X" && b == "X" && c == "X" || d == "X" && e == "X" && f == "X" || x == "X" && a == "X" && d == "X" || y == "X" && b == "X" && e == "X" || z == "X" && c == "X" && f == "X" || x == "X" && b == "X" && f == "X" || z == "X" && b == "X" && d == "X")
{
system("cls");
mainBoard();
cout << endl << "player 1 wins";
throw; // here is where the error is
}
}
//prints X to current players desired spot with user input
void exes()
{
caseCheck();
mainBoard();
cout << endl << "Player 1: pick a number of where you would like to go ";
cin >> currentMove;
system("cls");
if (cin.fail())
{
cin.clear();
cin.ignore();
mainBoard();
cout << endl << "cannot enter letter values" << endl;
exes();
}
switch (currentMove)
{
case 1:
//run check function for already inputted values
x = "X";
movesMade[0] = 1;
break;
case 2:
y = "X";
movesMade[1] = 2;
break;
case 3:
z = "X";
break;
case 4:
a = "X";
break;
case 5:
b = "X";
break;
case 6:
c = "X";
break;
case 7:
d = "X";
break;
case 8:
e = "X";
break;
case 9:
f = "X";
break;
default:
mainBoard();
cout << endl << "number is not located on tic tac toe board, try again" << endl;
exes();
break;
}
}
//prints O to current players desired spot with user input
void oes()
{
caseCheck();
mainBoard();
cout << endl << "Player 2: pick a number of where you would like to go ";
cin >> currentMove;
system("cls");
if (cin.fail())
{
cin.clear();
cin.ignore();
mainBoard();
cout << endl << "cannot enter letter values" << endl;
oes();
}
switch (currentMove)
{
case 1:
x = "O";
break;
case 2:
y = "O";
break;
case 3:
z = "O";
break;
case 4:
a = "O";
break;
case 5:
b = "O";
break;
case 6:
c = "O";
break;
case 7:
d = "O";
break;
case 8:
e = "O";
break;
case 9:
f = "O";
break;
default:
mainBoard();
cout << endl << "number is not located on tic tac toe board, try again" << endl;
oes();
break;
}
}
int main()
{
try
{
for (int i = 0; i < 4; i++)
{
cout << player;
exes();
oes();
}
exes();
mainBoard();
cout << "Tie game";
throw(player);
}
catch (...)
{
return 0;
}
}
在函数 caseCheck 中,我试图将异常抛出到主函数中,以便我可以停止程序。我尝试将出口放在那里或返回那里,但它没有做任何事情并且函数继续运行。该程序确实像它应该的那样停止了,但是在给我一个未处理的异常错误后它立即崩溃了。我应该怎么办?我应该如何以不同的方式处理这个问题?
标签: c++visual-studioexception
解决方案
推荐阅读