【问题标题】:My validate function keeps running although there's good input coming from user?尽管有来自用户的良好输入,我的验证功能仍在运行?
【发布时间】:2021-06-21 22:04:39
【问题描述】:

这是我的第一个堆栈溢出问题,所以如果我不是描述性的,请多多包涵。我的任务是创建一个非常简单的基于点数系统的骰子程序,例如如果你落在 1-12 的特定数字上,你要么赢一分,要么输。基本上就是这样。我的问题是,我应该放置一个验证函数,以防用户输入错误的输入。我让程序仅在用户输入“y”或“n”时运行,如果没有,它会提示用户输入正确的响应。

class Game
{
private:
  int die1;
  int die2;
  int dice;
  int totalPoints;
  int totalRolls;
  char ch;

  void prompt();
  void display();
  void validate();
  void rolls();



public:
  void driver();
  Game();
};

int main()
{

  Game dcObj;
  dcObj.driver();
  
  cout << "\n\n\n\n\n\n";
  
  return 0;
}

Game::Game()
{
  totalPoints = 0;
  totalRolls = 0;

  srand(time(NULL));
  die1 = rand() % 6 + 1;
  die2 = rand() % 6 + 1;

  dice = die1 + die2;
}

void Game::driver()
{
  prompt();
  display();
  
}

void Game::display()
{
  cout << "\n\nYou rolled the dice " << totalRolls << " times.";
  cout << "\nYou won " << totalPoints << " points.";

  if (totalPoints <= 0)
  {
      cout << "\n\nBetter luck next time!";
  }
  else
  cout << "Not bad!"; 
}

void Game::prompt()
{
  cout << "\nWelcome To My Game of Absolute Chance!";

  cout << "\n\nDo you want to roll? (y = yes, n = no) ";
  cin >> ch;

  validate();
}

void Game::validate()
{
  while (ch != 'n' || ch != 'y')
  {
      fseek(stdin, 0, SEEK_END);
      cin.clear();
      cout << "Invalid response. Please input (y = yes, n = no) ";
      cin >> ch;
  }
}

void Game::rolls()
{
  while (ch == 'y')
  {
      switch(dice)
      {
  case 2: cout << "Sorry, you rolled a " << dice << ", you lost.";
      totalPoints--;
      totalRolls++;
      cout << "Points: " << totalPoints;
      break;
  case 3: cout << "Sorry, you rolled a " << dice << ", you lost.";
      totalPoints--;
      totalRolls++;
      cout << "Points: " << totalPoints;
      break;
  case 4: cout << "You rolled a " << dice << ", you get nothing.";
      totalRolls++;
      cout << "Points: " << totalPoints;
      break;
  case 5: cout << "You rolled a " << dice << ", you get nothing.";
      totalRolls++;
      cout << "Points: " << totalPoints;
      break;
  case 6: cout << "You rolled a " << dice << ", you get nothing.";
      totalRolls++;
      cout << "Points: " << totalPoints;
      break;
  case 7: cout << "Congratulations! You rolled a " << dice << ", you win!";
      totalPoints++;
      totalRolls++;
      cout << "Points: " << totalPoints;
      break;
  case 8: cout << "You rolled a " << dice << ", you get nothing.";
      totalRolls++;
      cout << "Points: " << totalPoints;
      break;
  case 9: cout << "You rolled a " << dice << ", you get nothing.";
      totalRolls++;
      cout << "Points: " << totalPoints;
      break;
  case 10: cout << "You rolled a " << dice << ", you get nothing.";
      totalRolls++;
      cout << "Points: " << totalPoints;
      break;
  case 11: cout << "Congratulations! You rolled a " << dice << ", you win!";
      totalPoints++;
      totalRolls++;
      break;
  case 12: cout << "Sorry, you rolled a << " << dice << ", you lose.";
      totalPoints--;
      totalRolls++;
      break;
      }
  }

  if (ch == 'n')
  {
      display();
  }
}

我的问题是,无论输入是否良好,while 循环总是会运行。输入“y”或“n”仍会使其运行。我几乎尝试了所有事情。任何帮助都会很棒!

【问题讨论】:

  • ch 是成员变量吗?可以分享一下完整代码吗?
  • @DebojyotiMajumder 编辑了它。代码比较多,但剩下的代码是一个开关函数,将每个数字分配给积分系统
  • 你应该把所有的代码都放好,以便我们重现它。
  • @TudorTeo 整个代码都起来了!
  • 每个字符不是n或不是y。 “不是n 或不是y”可能为假的唯一方法是如果某事物既是n 又是y,这是不可能的。

标签: c++ validation input while-loop


【解决方案1】:

我认为问题在于您的验证功能:

void Game::validate()
{
  # you have a condition to start the while loop but not end it
  while (ch != 'n' || ch != 'y')  #using || will cause this to loop infinitely
  #as while loop continues until conditions are met
  #using && is more appropriate
  #as it will end if one of the condition is satisfied
  {
      fseek(stdin, 0, SEEK_END);
      cin.clear();
      cout << "Invalid response. Please input (y = yes, n = no) ";
      cin >> ch;
  }
}

我的偏好是接受正确的输入并拒绝其他任何内容,因为它更简单。 IMO,我会用 if else 来做一个更简单的程序。 我假设您声明的 ch 是一个字符而不是字符串,因为您的输入用 '.

括起来
void validate() {

    if(ch == 'y' && ch == 'n')
    {
        cout << "game start"; #add on call game function
    }
    else
        cout << "Invalid response. Please input (y = yes, n = no) ";
        cin >> ch;
    };

或者,按照你喜欢的方式:

void validate() {

    if(ch != 'y' && ch != 'n')
    {
        cout << "Invalid response. Please input (y = yes, n = no) ";
        cin >> ch;
    }
    else
        cout << "game start"; #add on call game function
};

【讨论】:

  • “每个字符都不是 n 或不是 y。“不是 n 或不是 y”唯一可能为假的方法是,如果某物既是 n 又是 y,这是不可能的”来自@David Schwartz跨度>
  • 哦,我忘了更改 &&,这与我一开始所说的相矛盾,我的错
【解决方案2】:

我认为问题在于您使用了“||”而是“&&”“||”表示“或”和“&&”表示“和”

【讨论】:

  • 哇,它成功了。非常欣赏它。现在我正在为当用户输入'y'时的while循环而苦苦挣扎,当我这样做时,它会永远持续下去。知道为什么吗?
  • 我认为它会永远持续下去,因为您在那段时间没有更改“ch”变量,因此它永远保持 ch='y';您也应该在其中放置一个要求“ch”值的 cin。
  • 成功了!我理解,因为 cin 语句在 while 循环之外,所以它只会询问一次,然后永远运行。谢谢!
猜你喜欢
  • 2012-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
相关资源
最近更新 更多