Problems with if and conditions

1

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;
}
    
asked by anonymous 09.03.2015 / 12:55

2 answers

2

I have reformulated your program to solve the problem and give it an organized one so that it becomes more readable. I recommend paying close attention to the changes.

I'm not sure if your code is doing what you want but I did tests with some values and it seems to give consistent results and solves the main problem you were complaining about. I organized the indentation and put the declaration of the variables inside the function since there is no reason for them to be out.

Variables must be declared in the innermost possible scope, in this case it is the function. The way it was the variables would work as "global" variables that is not usually a good idea. Of course in this restricted case does not cause any problem but in more complex cause codes. So you're learning the right thing right now.

I made a change by putting a loop for the code to repeat if the data you typed does not match the need. This way of using a% conditional% and an unconditional%% is not ideal but did not want to modify its logic, as you are learning you might have difficulty understanding. Basically this code inside block continue will run once and exit because of break . Unless it enters the while , then the break in it will cause if to be skipped, and will repeat the loop until it does not enter continue .

#include <iostream>
using namespace std;

int main() {
    int x, y, z, ft, sd, th;
    while(true) {
        cout << "Digite 3 valores reais e diferentes: \n";
        cin >> x;
        cin >> y;
        cin >> z;

        if (z == y || x == z || x == y) {
            cout << "\nErro. Os valores devem ser diferentes de 0 e nao repetidos. Tente novamente: \n" << endl;
            continue;
        }
        break;
    }
    if (x > y && x > z)
        ft = x;
    else if (x > y && x < z)
        sd = x;
    else if (x > z && x < y)
        sd = x;
    else if (x < y && x < z)
        th = x;

    if (y > x && y > z)
        ft = y;
    else if (y < x && y > z)
        sd = y;
    else if (y < z && y > x)
        sd = y;
    else if (y < x && y < z)
        th = y;

    if (z > x && z > y)
        ft = z;
    else if (z < x && z > y)
        sd = z;
    else if (z < y && z > x)
        sd = z;
    else if (z < x && 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;
}

See running on ideone .

Note that I do not ideone I put keys in break . They are not necessary in this case but it is advisable to always use it to avoid confusion when you need it.

    
11.03.2015 / 23:46
0

If you include these two lines before the 'main' statement, your code will compile without errors:

#define or ||
#define and &&
    
11.03.2015 / 12:14