vendredi 24 janvier 2020

Problems with Inputs to Char Datatype

Does anyone know why when I input more than one character in the "cInputCommandPrompt" it loops the "Press "Y" to continue, rather just showing it once such as what I'd like it to do. I've tried clearing the buffer? if that's what you call it but it just doesn't seem to work. If anyone could help me I'd greatly appreciate it. I basically want it so when the user doesn't inputs "Y" it just re-loops back to the start until they input the right one. It just doesn't like multiple character inputs which I have tried to sort.

void ContinueOptions()
{
    bool bValid = false;
    char cInputCommandPrompt = 0;
    do{
        std::cout << "Press ""y"" to continue: ";
        std::cin >> cInputCommandPrompt;
        cInputCommandPrompt = std::toupper(static_cast<unsigned char>(cInputCommandPrompt));

        if (!std::cin >> cInputCommandPrompt)
        {

            std::cin.clear();
            std::cin.ignore(100);
            std::cout << "Please try again.";
        }
        else if (cInputCommandPrompt == 'Y')
        {
            bValid = true;
        }
    }while(bValid == false);
    std::cout << "\n";
}

Aucun commentaire:

Enregistrer un commentaire