Problems with " or " in C++

I need to make an algorithm that receives 3 different numbers, and if it receives repeated numbers, it reports an error message.

My program is all working properly, but when I put such a line to warn of the error I try to use Or and it does not work. Follow what I'm typing

if (z==x) or (y==z) or (x==y);
    cout << "\nErro. Os valores devem ser diferentes. Tente novamente: \n" << endl;

Even though it has an easier mode than or, could you please tell me how does inserting or in C++ work ?

According to Code:: Blocks, the program expects a primary expression before or.

Author: Maniero, 2015-03-06

3 answers

The preferred syntax of or in C++ is || even though they are synonymous.

In addition to this had a ; that was terminating the if and not executing as it seems to be your desire. The message was not part of the conditional block.

There is also a problem with parentheses. A single pair should contain the entire condition. You could even put extra parentheses in the sub-expressions but they are totally unnecessary.

Then your line would be:

if (z==x || y==z || x==y)
    cout << "\nErro. Os valores devem ser diferentes. Tente novamente: \n" << endl;

I put on GitHub for future reference .

Keys could be recommended to encapsulate the block even if it has only one row. This avoids some careless mistakes. Even if you choose not to use keys it would be interesting to put the block that should be executed conditionally on a new line with indentation as shown above.

 16
Author: Maniero, 2019-08-08 18:35:00

The logical operator or in C++ is ||.

if(z == x || y == z || x == y) {
  cout << "\nErro. Os valores devem ser diferentes. Tente novamente: \n" << endl;
}

You are also adding a ; after the if. This will cause this line to be terminated there and the cout will always print the message, regardless of the result of the if evaluation.

 4
Author: André Ribeiro, 2015-03-06 00:20:51

If you are going to use C++ 11/14, you can use or( || ) and and( && ) normally if you want.

 0
Author: Matheus Catarino França, 2016-06-23 16:55:39