Hey, guy I'm having some trouble with my function see below:

bool tryAgain()
{
    char answer;

    cout << "Do you want to enter another number? (Y/N): ";
    cin >> answer;
    answer = toupper(answer);

    while(answer != 'Y' || answer != 'N')
    {
        cin.clear();
        cin.ignore(10, '\n');
        cout << "Input invalid." << endl;
        cout << "Do you want to enter another number? (Y/N): ";
        cin >> answer;
        answer = toupper(answer);
    }

    if(answer == 'Y')
    {
        return true;
    }
    else
    {
        return false;
    }
}

the function enters the while loop even when a correct answer is inputted from the get go & if I do enter the while loop I get stuck even though I enter a correct answer. To me this logic makes sense, break the loop when the input is == 'Y' || 'N', but its keeps looping. Help me see my logical error! Thanks for your help.

Recommended Answers

All 3 Replies

You want both conditions to be true; that requires the && operator. What you are asking is for is for either condition to be true (which will always be the case since answer can not be both values at the same time).

while(answer != 'Y' && answer != 'N')

or

while(!(answer == 'Y' || answer == 'N'))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.