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

Feb 17, 2013 at 12:38am
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;

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

It works with <while (answer != 'y');> but if i put "or" operators the loop never stops.
Thanks in advance
Feb 17, 2013 at 12:45am
You need "and", not "or".
Feb 17, 2013 at 12:46am
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.