0

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.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
Ryan Swann
  • 91
  • 5
  • Why does nobody ever include their headers and `using namespace std;` when posting examples? Surely it's more work to **not** copy them? – user657267 Oct 23 '15 at 09:08
  • I just thought because it was a simple application it was fairly obvious what the headers were. – Ryan Swann Oct 23 '15 at 09:10
  • It's both matter of convenience for us, because playing the hunt the header game is no fun, and of providing a correct, complete example. It's highly unlikely you have an error your `include`s but you really should make sure to post everything. Posting everything makes us faster, which means you'll get your answer sooner. – user657267 Oct 23 '15 at 09:12
  • Ah, fair enough then. Sorry about that I'll do it in the future. – Ryan Swann Oct 23 '15 at 09:15

3 Answers3

0

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.

Jérôme
  • 8,016
  • 4
  • 29
  • 35
  • 1
    Thank you for this, it's great! Do you have any advice on learning C++? I see on your profile that you are quite proficient with the language. Thanks! – Ryan Swann Oct 23 '15 at 10:10
  • @RyanSwann Thanks! The best way to learn C++ is to keep doing what you started: practise coding with exercise (this is true for every language). You can also start reading books - There is a good post about C++ books: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Jérôme Oct 23 '15 at 10:13
0

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

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. :)

Asthea
  • 1
  • 2
0

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.

Community
  • 1
  • 1
Dese
  • 338
  • 1
  • 12