0

For the following, Im trying to limit the users input to only Y or y or N or n. Please follow my comments on the codes so I can point out what the problem is. I'm very new to this forum, I have a lot of passion for programming, please help me if anyone can. THANK YOU. The while loop(not the do-while loop) is the part I'm having trouble with. I think maybe I didn't use the != correctly. I haven't anything too advance yet, the class I'm in right now is just introductory level.

    cout << "Would you like to use this program again?: ",
    cin >> ans;

    if(ans =='Y'||ans =='y'||ans =='N'||ans =='n')
        break;
    else //This is where I'm having problem with.
        while (ans != 'Y'||ans != 'y'||ans !='N'||ans !='n')
        {
            cout << "Please enter Y or y if you like to use the program again and N or n do exit.",
            cin >> ans; //If the question is asked and no matter what I input for ans, the while loop never gets exited. Why? Is there something I didn't use right?
        }
}while (ans == 'Y'||ans =='y');

return 0;
Robin
  • 1
  • 1

1 Answers1

0

A better way to handle your logic would be to have a single do loop which continually prompts the user for yes/no input until he gives it:

do {
    cout << "Please enter Y or y if you like to use the program again and N or n do exit.",
    cin >> ans;
} while (ans != 'Y' || ans != 'y' || ans !='N' || ans !='n');
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360