【问题标题】:This code is not working properly and it doesn't make any sense why it isn't to me. Does anyone have a guess what the problem might be?这段代码不能正常工作,为什么它对我来说没有任何意义。有谁猜出问题可能是什么?
【发布时间】:2021-01-10 01:18:25
【问题描述】:

This 是我正在制作的 C++ 刽子手游戏的链接。它在repl 上工作得非常好,但是当我尝试构建和运行它时它不起作用。它的字母在游戏一开始就被猜到了,显然你没有猜到。这些字母也相当随意,与游戏或任何东西无关,至少我认为。其他人至少可以在他们的计算机上运行它,看看它是否只有我?谢谢!开头是:

WELCOME TO HANGMAN!!


----------------------------------------------------------------------------------------------------------

Here are the rules:
* you must enter one letter
* that letter cannot be a symbol
* you have ten guesses
* correct guesses won't count against you
* you cannot guess the same letter twice
* and finally, if you need to give up enter any numeral



You have 10 incorrect guesses left
The word so far is

_ _ _ _ _


You have guessed: A, B, I, J, K, Q, R, T,
What is your guess?

这是第一次打开时确实给出的信息,但是,我希望它表明您还没有猜到任何东西,因为您还没有猜到。

cout << "You have " << guesses << " incorrect guesses left" << endl;
    cout << "The word so far is\n\n";
    for (int i = 0; i < word.length(); i++)
    {
      if (i == word.length() - 1)
      {
        cout << guessedWord[i] << endl;
      }
      else
      {
        cout << guessedWord[i] << " ";
      }
    }
    cout << "\n\nYou have guessed: ";
    for (int i = 0; i < guessed.size(); ++i)
    {
      if (guessed[i])
      {
        cout << static_cast<char>(i + 'A') << ", ";
      }
    }
    cout << "\nWhat is your guess?" << endl;
    cin >> myGuess;
    istringstream myGuess2(myGuess);
    char guess = toupper(myGuess2.peek());
    if (isalpha(guess) == false)
    {
      if (isdigit(guess) == true)
      {
        break;
      }
      else
      {
        cout << "That is not a letter, please try again" << endl;
        continue;
      }
    }
    else if (guessed[guess - 'A'])
    {
      cout << "You have already guessed this." << endl;
      continue;
    }

这就是它的那个部分的样子,如果你没有想要放弃的条件(如果你输入一个数字)或者你没有输入一个实际的字母字符,那么它会继续采用你猜的 ascii 值,减去 'A' 的 ascii 值,得到字母在布尔数组中的位置,然后将该字母的值设置为 true。之后它只是检查您的猜测是否正确以及所有内容,但现在可以正常工作。

【问题讨论】:

  • 请将您的原始源代码直接复制并粘贴到问题正文中。另外,请给出输入、预期输出和实际输出。
  • “原始源代码”是什么意思?
  • 请将您的代码(到问题中)作为文本发布。防火墙阻止我点击您的链接。
  • 重现问题的相关代码必须是您问题的一部分,而不是外部链接。这里提出的问题不仅应该帮助提出问题的人,而且应该帮助任何未来的读者。因此,要求问题包含所有相关信息,而无需依赖可能随时无法访问的外部链接。

标签: c++ random


【解决方案1】:

您仍然没有在问题中包含所有相关代码。

游戏一开始就已经猜到了字母,什么时候,显然你没有猜到。

问题在于您的代码:

array<bool, 26> guessed;

这不会将array 的元素设置为false,它们具有不确定的值。

如果你希望他们是false,你需要写:

array<bool, 26> guessed{};

为避免此类问题,您可以使用 AAA 样式(几乎总是自动),并以这种方式定义变量:

auto guessed = array<bool, 26>();

【讨论】:

    猜你喜欢
    • 2021-07-14
    • 1970-01-01
    • 2021-10-24
    • 2012-06-10
    • 2018-05-05
    • 2022-12-16
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多