简体   繁体   中英

Cannot work out the last of this little C++ program that prints numbers in numerical sequence

I am currently going through "Programming: Principles and Practice Using C++" and I cannot quite work out one particular exercise. In this program the user will enter three random numbers and then the program will return those numbers in numerical sequence. The first and second numbers return correctly if they are entered as the lowest but the third one doesn't and I am unsure as to why. Here is the code:

int main()
{
    string barn;



    int val1 = 0, val2 = 0, val3 = 0;

    cout << "Enter the first number: ";
    cin >> val1;

    cout << "Enter the second number: ";
    cin >> val2;

    cout << "Enter the third number: ";
    cin >> val3;


    if (val1 < val2 && val3)
    {
        cout << val1 << " "; 

        if (val1 < val2)
        {
            cout << val2 << " ";
            cout << val3 << " ";
        }

        else
        {
            cout << val3 << " ";
            cout << val2 << " ";
        }
    }

    else if (val2 < val1 && val3)
    {
        cout << val2 << " ";

        if (val2 < val1)
        {
            cout << val1 << " ";
            cout << val3 << " ";
        }

        else
        {
            cout << val3 << " ";
            cout << val1 << " ";
        }
    }

    else if(val3 < val1 && val2)
    {
        cout << val3 << " ";

        if (val3 < val1)
        {
            cout << val1 << " ";
            cout << val2 << " ";
        }

        else
        {
            cout << val2 << " ";
            cout << val1 << " ";
        }
    }

    cin >> barn;
}

1 3 5 prints: 1 3 5. 3 1 5 prints: 1 3 5. 3 5 1 prints: 3 5 1` - This is where my issue lies. The second else if statement does not appear to be working properly and I am not sure why.

This does not do what you think:

if (val1 < val2 && val3)

Your if condition is true when ( val1 is greater than val2 ) AND ( val3 evaluate to something different from 0 )

you should do if (val1 < val2 && val1 < val3)

Same rule applies for other if statement.

Also your inner if statement are not correct. Example: once you check that val1 < val2 && val1 < val3 , you should check that val2 < val3 . Below an example for your first if statement:

if (val1 < val2 && val1 < val3)
{
    cout << val1 << " "; 

    if (val2 < val3)
    {
        cout << val2 << " ";
        cout << val3 << " ";
    }

    else
    {
        cout << val3 << " ";
        cout << val2 << " ";
    }
}

Finally you should think of the case where the user enter several time the same value.

Btw, 351 won't get you into the third else if statement. Check your operator precedence or write your operators out completely, eg:

else if ((val3 < val1) && (val3 < val2)) 

also see: http://en.cppreference.com/w/cpp/language/operator_precedence

Imo you should always write out your operators completely, especially if you're new to the language, it really helps. :)

This:

if(val1 < val2 && val3){...}

doesn't mean what you think it does. The correct way to write this is:

if((val1 < val2) && (val1 < val3)){...}

The extra parentheses may not be necessary, but my rule in this case is better be safe than sorry :)

What:

if(val1 < val2 && val3){...}

does is that it returns true if the booleans (val1 < val2) and (val3) are true. (val3) is evaluated as true if val3 is non-zero (**cf EDIT section), else it is evaluated as false.

Change your code to take that into account then tell us if it works, I didn't check if the rest was correct.

EDIT

My answer was edited to say that any value other than 0 is evaluated as true.

That's right, thank you :) I think that it might be useful to others to know what misled me.

  1. I couldn't find any reference to that in the C++ documentation, but well it is the norm in every language I suppose.
  2. The expression (5 == true) returns false, but (5) is evaluated as true. I suppose that's because in (5 == true), true is cast to the int value 1 and the comparison is between int values, not booleans. (true == 5) also returns false.

This is certainly a beginner's mistake, I take that as a lesson to make explicit condition tests, and never trust the program to evaluate them as I would want it to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM