lundi 16 novembre 2020

Endless loop - c++

so i have this function that i need to put, two inputs. char and int. Im asking user to put inputs. Now if the inputs are not right, then the program stock with endless loop. how can i solve this? the problem happen when i enter to Isvalid, then if for example i insert sign = d, value d, then there is endless loop, and the line "Enter Valid Card (S/C/H/D) and value: " just keep repeating.

Card* gameEngine::handToSet(Player *p, int setIndx)
{
    Card *usedCard;
    char sign;
    int value, indx;
    cout << "Which card? enter sign (S/C/H/D) and value:    ";
    cin >> sign >> value;
    while (1)
    {
        if(isValid(sign, value))
        {
            indx = p->searchCardinHand(sign, value);
            if (indx >= 0)
            {
                usedCard = boardSets[setIndx]->addCard(p->extructCard(sign, value));
                return usedCard;
            }
        }
        cout << "Enter Valid Card (S/C/H/D) and value:   ";
        cin >> sign >> value;
    }
}

bool gameEngine::isValid(char sign, int val)
{
    if (sign == 'C' || sign == 'S' || sign == 'H' || sign == 'D')
        if (val <= 13 || val >= 1)
            return true;
    return false;   
}

Aucun commentaire:

Enregistrer un commentaire