Mitcy
Mitcy

Reputation: 11

Infinite while loop - char to close not working C++

The line:

cin >> cRestart;

is capturing the char, but the while loop is not terminating. I need the code to move on. Can you help please? (The while loop looks for y, Y, n and N in case you can't see it all.)

    bool startInputValidation()
    {
        char cRestart = 'b';            //set initial value at b to run while loop

        conRGB(COLOUR_WHITE);           //text colour white

        conXY(0, 19);                   //cursor position in console
        drawLine();                     //draws a horizontal line across screen
        conXY(0, 23);
        drawLine();

        while (cRestart != 'y' || cRestart != 'Y' || cRestart != 'n' || cRestart != 'N')    //check for correct input (Y/N)
        {
            conXY(21, 21);
            for(int iCount = 0; iCount < 139; iCount++)                                     //blank lines
            {
               cout << " ";
            }
            conXY(21, 21);
            cout << "ARE YOU SURE YOU WISH TO CONTINUE Y/N?   ";                                    //ask question
            cin >> cRestart;                                                                //get input from user
        }

        if (cRestart == 'y' || cRestart == 'Y')                                             //if yes return true
        {
            return true;
        }
        else
        {
            return false;
        }
    }

Upvotes: 1

Views: 110

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311058

This control structure

    char cRestart = 'b';            //set initial value at b to run while loop
    // ...

    while (cRestart != 'y' || cRestart != 'Y' || cRestart != 'n' || cRestart != 'N')    //check for correct input (Y/N)
    {
        conXY(21, 21);
        for(int iCount = 0; iCount < 139; iCount++)                                     //blank lines
        {
           cout << " ";
        }
        conXY(21, 21);
        cout << "ARE YOU SURE YOU WISH TO CONTINUE Y/N?   ";                                    //ask question
        cin >> cRestart;                                                                //get input from user
    }

1) is invalid ( it has invalid condition in the while loop 2) looks badly ( its better to use another control structure)

I would write the function the following way

bool startInputValidation()
{
    char cRestart;                  //set initial value at b to run while loop

    conRGB(COLOUR_WHITE);           //text colour white

    conXY(0, 19);                   //cursor position in console
    drawLine();                     //draws a horizontal line across screen
    conXY(0, 23);
    drawLine();


    do
    {
        conXY(21, 21);
        for ( int iCount = 0; iCount < 139; iCount++ )
        {
           cout << ' ';
        }

        conXY(21, 21);
        cout << "ARE YOU SURE YOU WISH TO CONTINUE Y/N?   ";

        cin >> cRestart;
        cRestart = toupper( cRestart );
    } while ( cRestart != 'N' && cRestart != 'Y' )

    return cRestart == 'Y';
}

Upvotes: 0

Barmar
Barmar

Reputation: 781721

Sigh, another programmer who has forgotten deMorgan's Law. it should be:

    while (cRestart != 'y' && cRestart != 'Y' && cRestart != 'n' && cRestart != 'N')    //check for correct input (Y/N)

Upvotes: 3

Related Questions