Checking if 3 Variables are Equal

I need help with a program I am making. The user inputs the values to 3 variables. I need the loop to stop when the variables are equal.
I tried putting:

1
2
3
4
5
6
int main()
{
  while (variable1 != variable2)&(variable2 != variable3){
//code
}
}

but it didn't work, because the loop ended when 1 and 2 were equal. Can you help?
I have a limited c++ knowledge but I am quick to learn. Thanks.
Firstly the 'and' operator is '&&'.
'&' is the bitwise operator, which you don't want.
Another thing to mention is you would put them all within the parenthesis () of the while loop.
Try do while loop guess will do..single and '&' wil do also but it is better if &&..
Try do while loop guess will do..single and '&' wil do also but it is better if &&..


eh? single & will NOT do..
&& is wrong. If you use && all three variables must be not equal in order to loop.

Use ||:
while (variable1 != variable2)||(variable2 != variable3){ will stop when all variables are equal
i know the logic of && will be wrong, but that guys advising using bit-wise and.
@mutexe I accidentally put single & in my code but I did not get error and the code worked.

Coder777 is right TRUE or TRUE = TRUE then code stop
I accidentally put single & in my code but I did not get error and the code worked.
but that doesn't meant your code was correct.

Don't interchange bitwise and logical operators just because it appears to work. Sooner or later you'll trip yourself up that way.
Bitwise AND might not be to recommend but as long as both operands are bool it works fine.
As long as both operands are bool - yes, that's correct.

However, very often logical tests are carried out on other types, which is why it's good practise to keep the appropriate operators for the appropriate purpose.
1
2
3
4
5
6
7
8
9
10
11
12
    int A = 1;
    int B = 2;
    
    if (A && B) 
        cout << "&& ok"   << endl;
    else
        cout << "&& fail" << endl;  
           
    if (A & B) 
        cout << "& ok"   << endl;
    else
        cout << "& fail" << endl; 
&& ok
& fail


Last edited on
Damn as that simple I cannot :( this not my thread
Last edited on
sorry the single '&' was a typo. in the code there are 2 &'s
1
2
3
4
5
6
while(variable1 != variable2 || variable2 != variable3)
{
     //get the inputs again
     std::cout << "Please enter 3 values: ";
     std::cin >> varaible1 >> variable2 >> variable3;
}
Last edited on
Topic archived. No new replies allowed.