Home > Net >  How can reduce recursion within this function
How can reduce recursion within this function

Time:10-08

can't figure out how to prevent recursion within this function, any suggestions or recommendations? I've tried a couple of my own solutions but ran into the error of having the program crash if wrong inputs and repeatedly entered. Thanks in advance, DK.

static void CheckPlayerInput()
{
    // gets the character entered (as a char)
    cin >> input;

    //check is actually a char
    if (cin.fail())
    {
        //more recursion, could cause issues, could you find another way?
        cout << "Invalid character, try again!" << endl;
        CheckPlayerInput();
    }

    //converts char to upper case
    input = toupper(input);

    //check the input character
    // maps the input keys to the grid reference
    //calling the FillSquare method
    switch (input)
    {
    case '1': FillSquare(0, 0); break; //top-left
    case '2': FillSquare(0, 1); break; //top-centre
    case '3': FillSquare(0, 2); break; //top-right
    case '4': FillSquare(1, 0); break; //middle-left
    case '5': FillSquare(1, 1); break; //middle-centre
    case '6': FillSquare(1, 2); break; //middle-right
    case '7': FillSquare(2, 0); break; //bottom-left
    case '8': FillSquare(2, 1); break; //bottom-centre
    case '9': FillSquare(2, 2); break; //bottom-right
        //IF NOT ANY OF THE ABOVE
    default:
    {
        //more recursion, could cause issues
        //
        cout << "Invalid character, try again!" << endl;
        CheckPlayerInput();
    }
    break;
    }

}

CodePudding user response:

Use a do while loop instead

static void CheckPlayerInput()
{
    bool invalid = false;
    do
    {
        invalid = false;
        // gets the character entered (as a char)
        cin >> input;
...
        default:
        {
            //more recursion, could cause issues
            //
            cout << "Invalid character, try again!" << endl;
            invalid = true;
        }
        break;
      }
    }
    while(invalid);
}

The while loop will repeat as long as the input is invalid

CodePudding user response:

I guess there are many different ways to implement it. The below might be my approach.

Note:

  • Filling the square feels like a dedicated task and should therefore be inside it's own function.
  • Maybe you should use non-static / non-global methods and instead use objects.
  • Your method CheckPlayerInput is not checking the input. Instead, it's processing it.
static bool 
_fillSquare(char type)
{
    switch (type)
    {
    case '1': FillSquare(0, 0); break; //top-left
    case '2': FillSquare(0, 1); break; //top-centre
    case '3': FillSquare(0, 2); break; //top-right
    case '4': FillSquare(1, 0); break; //middle-left
    case '5': FillSquare(1, 1); break; //middle-centre
    case '6': FillSquare(1, 2); break; //middle-right
    case '7': FillSquare(2, 0); break; //bottom-left
    case '8': FillSquare(2, 1); break; //bottom-centre
    case '9': FillSquare(2, 2); break; //bottom-right

    default : return false;
    }
    
    return true;
}


// this method does not `check` the player input, but it
// `processes` it. The name should reflect that.
static void 
processPlayerInput()
{
   // try to retrieve and process input until 
   //  a valid input was given

   while (true)
   {
      cin >> input;                
      
      if (!cin.fail ())
         continue;
      
      input = toupper(input);
      bool success = _fillSquare(input);
      if (!success)
      {
         cout << "Invalid character, try again!" << endl;
         continue;
      }
      
      return;           
   }
}

  • Related