Problems with if and conditions

Asked

Viewed 520 times

1

I’m making a code that receives three values and orders them from the highest to the lowest. As a condition, when the numbers are repeated, an error message is displayed and the program must close. If the numbers are distinct, it skips this if and proceed to the actual ordination. 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. Values should be different. Try again:"

I would like it to be displayed only when there was the error, and in this case, the program should end right there, and not continue, because this is also occurring.

Below follows 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;
}
  • 7

    You’re confused, mainly because you’re not putting in your real code. But it actually seems that you ignored the answers given to you in the other question. If you insist on doing wrong it will not work. http://answall.com/questions/53261/problemas-com-or-em-c. You must [Edit] the question and place your code to be analysed, please read this http://answall.com/help/mcve

  • 2

    As you said 'getting the hang of Sopt', I will suggest that you read the [TOUR]. And the link @bigown posted is also quite interesting for you to find out how Sopt works.

2 answers

2


I reformulated his program to solve the problem and give an organized to make it 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 some tests with some values and seems to give consistent results and solves the main problem you were complaining about. I organized the indentation and put the declaration of variables inside the function since there is no reason for them to be outside.

Variables must be declared in the most internal possible scope, in this case it is the function. The way it was, the variables would work as "global" variables, which isn’t usually a good idea. Of course in this restricted case does not cause any problem but in more complex codes cause. So you are already learning the right.

I made a change by placing a loop for the code to repeat if the entered data is not according to the need. This way of using a continue conditional and a break 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 the block while will run once and leave because of the break. Unless he goes into if, there the continue on it will make the break be skipped, and will repeat the loop until no longer enter the if.

#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;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

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

  • It didn’t work here. It presented the same error of continuing with subsequent if’s even if the numbers are repeated, and in this case it still generates gigantic values in the ordering. Could be my IDE?

  • 1

    I believe it’s not the IDE’s problem. It’s hard to say for sure, you’ve seen that it’s working, right? Have you tried copying and pasting this code here and compiling it instead of changing yours? If only to test.

  • 1

    @Maximilianomeyer had something I hadn’t realized, I’ve changed now see if it solves what you want. Then if you evolve and have other questions, post here for us to help you in new questions.

  • Ball show. Working. I will accept the answer. Hug

  • Now he’s been forced

-1

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

#define or ||
#define and &&
  • Isalon returned the following message: Error: "and" cannot be used as a macro name as it an Operator in c++

  • 2

    This is highly not recommended. He is learning. Other people without understanding the context will come here and think they should do this for real. Honestly I’m thinking of negativizing the answer because it induces error. It works but no one should do it except as a joke.

  • 1

    @Bigown What surprises me is the positive vote. I respect and defend the right of lsalamon to post whatever you want (and bear the natural consequences), but the vote worries me more than the answer.

  • Afff, I’ve been fooled =(

  • 1

    @Maximiliano Meyer, do not take into consideration the above comments. I put your code in Visual Studio 2013 and compiles perfectly with my change. Believe me.

  • Huum. Okay, I do. I’m here to learn, not to judge. Thank you then.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.