简体   繁体   中英

My validate function keeps running although there's good input coming from user?

This is my first stack overflow question, so please bear with me if I'm not descriptive. My assignment is to create a very simple dice program that's based on a point system, eg if you land on a specific number from 1-12, you either win a point, or lose. That's basically all it is. My problem is that, I'm supposed to put a validate function just in case the user put in the wrong input. I made the program to run only if the user input 'y' or 'n', and if not, it will prompt the user to to put the correct response.

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();
  }
}

My issue is that, regardless if there's good input or not, that while loop always runs. Putting 'y' or 'n' will still make it run. I've attempted about almost everything. Any help will be great!

I think the problem is that you used "||" instead "&&" "||" means "or" and"&&" means "and"

I believe the issue lies in your validation function:

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;
  }
}

My preference is to accept the correct input and deny anything else because it is much more simpler. IMO, I would use if else for a simpler program. I assume the ch you declared is a char instead of string as your input is enclosed with '.

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;
    };

or, to follow how u like it:

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
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM