or(||) doesn't seem to work on loop

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;

int main (){
cout << "2+2=4?" << endl;
char answer;
cin >> answer;
if(answer=='y' || answer=='Y'){
cout << "right" << endl;
else if(answer=='n' || answer=='N'){
cout << "wrong" << endl;
cout << "answer the question" << endl;
while (answer != 'y' || answer != 'Y' || answer != 'n' || answer != 'N');
return 0;

It works with <while (answer != 'y');> but if i put "or" operators the loop never stops.
Thanks in advance
You need "and", not "or".
Your loop will continue as long as any one of the four conditions in the while statement are true. If answer == 'y', then the first condition is false, but the remaining three conditions are all true and your loop continues. Likewise for any of the other conditions.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
Topic archived. No new replies allowed.