c++嵌套循环无限重复(c++类项目)
C++ nested loops repeat infinitely (C++ class project)
我在为我的c++类编写一个老虎机程序时遇到了一个小问题。
该函数"完全按照字面意思执行",也就是说:它的目的是允许用户从struct数组中删除其他用户。
在为变量sureDelete输入值时出现了问题。由于某种原因,在为它输入一个值之后,程序回到我的"有效槽检查"do/while循环中的第一个while循环;也就是说,它重复执行第24行中的输出语句。
在代码底部附近(第57-62行),我还注释掉了另一个while循环,它也只是无限循环输出。
这里的共同主题是,我真的不明白为什么会发生这种情况;在这两种情况下,我的测试输入都是这样的,循环条件检查的值应该是false,因此不执行循环。在尝试了几个小时的调试后,我太沮丧了,想不出更多的想法,所以我在这里。
void deleteUser(playerData players[], const int NUM_OF_PLAYERS, int playerIndex)
{
int index, deletionIndex, sureDelete, choice;
bool validSlot = false;
do
{
std::cout << "PLAYER DATA LIST: " << std::endl << std::endl;
for (index = 0; index < playerIndex; index++)
{
std::cout << "PLAYER " << (index + 1) << ": " << std::setw(8) << players[index].name << std::setw(8) << players[index].score << std::endl;
}
std::cout << "nnEnter the slot number of the player whose data you wish to delete: ";
std::cin >> deletionIndex;
deletionIndex -= 1; // Adjusts for the user's "slot number" entry; Data slot 1 = index 0, etc.
do
{
while (deletionIndex < 0 || deletionIndex > NUM_OF_PLAYERS)
{ // Checks whether the user picked a slot with the range of the players array. If not, ask them to pick another slot
std::cout << "Invalid slot number. Please enter a slot between 1 and " << NUM_OF_PLAYERS << ": ";
std::cin >> deletionIndex;
std::cout << std::endl;
deletionIndex -= 1;
}
while (players[deletionIndex].name == "" && players[deletionIndex].score == 0)
{ // Checks whether the slot the user picked is already empty. If it is, ask them to pick another slot
std::cout << "That slot is already empty! Please select another slot: ";
std::cin >> deletionIndex;
std::cout << std::endl;
deletionIndex -= 1; // Same adjustment mentioned above
}
// Checks whether the deletion index entered after the second while loop still satisfies the condition of the first while loop.
// If it does, end the "slot validity check" loop
if (deletionIndex >= 0 && deletionIndex < NUM_OF_PLAYERS)
validSlot = true;
} while (!validSlot);
std::cout << "nAre you sure you wish to delete the data in slot " << (deletionIndex + 1) << "? (Y/N): "; // Ensures there will be no "accidental deletion" of data due to typos, etc
std::cin >> sureDelete;
// Checks whether the user entered valid input BEFORE executing the switch structure.
// If they didn't, it requests new input until they do so, at which point the program continues to the switch structure.
// This negates the need for a nested do/while loop and a "default" case in the switch
//while ((sureDelete != 'y' && sureDelete != 'Y') && (sureDelete != 'n' && sureDelete != 'N')){
//std::cout << "Invalid input." << std::endl << std::endl
//<< "Are you sure you wish to delete the data in slot " << (deletionIndex + 1) << "? (Y/N): ";
//std::cin >> sureDelete;
//}
switch (sureDelete)
{
case 'Y':
case 'y':
players[deletionIndex].name = "";
players[deletionIndex].score = 0;
std::cout << "Player data deleted." << std::endl;
break;
case 'N':
case 'n':
break; // If user does not wish to delete data in slot, do nothing
default:
std::cout << "Invalid input.";
}
std::cout << "nnWould you like to delete another player's data? (Y/N): ";
std::cin >> choice;
} while (choice != 'n' && choice != 'N');
}
sureDelete
被声明为int
,因此std::cin >> sureDelete
将期望输入数字。如果此时输入字母(如y
或n
), cin
将进入坏状态,不允许您读取任何内容。您可以尝试将其声明为char
作为快速修复。
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 既然存在危险,为什么项目要使用-I include开关
- cmake在我的项目中所需的所有静态库都不成功
- QT通过C++添加映射QML项目
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在linux上调试巨大的C++项目
- 在其他文件中创建类时在 c++ 项目中不起作用
- 使外部项目可用于find_package CMake
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- 如何维护资源管理器项目视图中当前可见的项目列表
- 错误-我无法在VS2019中打开新的Qt项目
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗