c++嵌套循环无限重复(c++类项目)

C++ nested loops repeat infinitely (C++ class project)

本文关键字:c++ 项目 嵌套循环 无限      更新时间:2023-10-16

我在为我的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将期望输入数字。如果此时输入字母(如yn), cin将进入坏状态,不允许您读取任何内容。您可以尝试将其声明为char作为快速修复。