I am making a code that gets three values and sorts them from highest to lowest. As a condition, when the numbers are repeated, an error message is displayed and the program should quit. If the numbers are different, it skips this if
and goes to the ordering itself.
I used the following line of code:
if (z==x or y==z or x==y)
cout << "\nErro. Os valores devem ser diferentes. Tente novamente: \n" << endl;
//restante do código
However, even if the numbers are different, the program always displays the message
"Error: The values must be different, try again:"
I would like it to be displayed only when there is an error, and in that case, the program terminated right there, and did not continue, as this is also happening.
Below is the complete code:
#include <iostream>
using namespace std;
int x,y,z,ft,sd,th;
int main()
{
cout << "Digite 3 valores reais e diferenteees: \n";
cin >> x;
cin >> y;
cin >> z;
if (z==y or x==z or x==y)
cout << "\nErro. Os valores devem ser diferentes de 0 e nao repetidos. Tente novamente: \n" << endl;
if (x>y and x>z)
ft=x;
else if (x>y and x<z)
sd=x;
else if (x>z and x<y)
sd=x;
else if (x<y and x<z)
th=x;
if (y>x and y>z)
ft=y;
else if (y<x and y>z)
sd=y;
else if (y<z and y>x)
sd=y;
else if (y<x and y<z)
th=y;
if (z>x and z>y)
ft=z;
else if (z<x and z>y)
sd=z;
else if (z<y and z>x)
sd=z;
else if (z<x and z<y)
th=z;
cout << "\nOs valores ordenados sao: \n" << endl;
cout << "Valor mais alto: " << ft<< endl;
cout << "Valor intermediario: " << sd<< endl;
cout << "Valor mais baixo: " << th<< endl;
return 0;
}