好的,我这里有问题。我正在制作一个幻灯片益智游戏。玩家被问到他想要移动哪一块,直到谜题被解决。如果玩家之前想退出,输入Q
或q
按即可enter
。该程序运行良好。但是我有一个问题:如果我插入CTRL+Z
,程序会loop
出乎意料地......
这是一段重要的代码:
// analyzes user input
if (piece_to_move_string == "q" ||
piece_to_move_string == "Q")
{
cout << endl << "You chose to quit." << endl;
pressanykey();
break;
}
else
{
piece_to_move = atoi(piece_to_move_string.c_str());
if (1 <= piece_to_move && piece_to_move <= pow(puzzle_size,puzzle_size))
{
game_board = move_piece (game_board, piece_to_move);
}
else
{
cout << "Not possible.";
}
}
编辑:但仍然不起作用..
// analyzes user input
if (piece_to_move_string == "q" ||
piece_to_move_string == "Q")
{
cout << endl << "You chose to quit." << endl;
pressanykey();
break;
}
else if (cin.eof())
{
//do nothing
}
else
{
piece_to_move = atoi(piece_to_move_string.c_str());
if (1 <= piece_to_move && piece_to_move <= pow(puzzle_size,puzzle_size))
{
game_board = move_piece (game_board, piece_to_move);
}
else
{
cout << "Not possible.";
}
}