Question: I'm a C++ beginner and I was told to make a program to arrange three integers from least to greatest. How did I do?

```
#include <iostream>

using namespace std;

int main()
{
    int x = 0;
    int y = 0;
    int z = 0;
    int first = 0;
    int second = 0;
    int third = 0;
    cout<<"Enter three integers to be arranged from least to greatest.\n";
    cin>>x>>y>>z;

    if(x <= y)
    {
        if(x <= z)
        {
            first = x;
            if(y <= z)
            {
                second = y;
                third = z;
            }
            else
            {
                third = y;
                second = z;
            }
        }
        else
        {
            second = x;
            if(y <= z)
            {
                first = y;
                third = z;
            }
            else
            {
                third = y;
                first = z;
            }
        }
    }
    else
    {
        if(x <= z)
        {
            second = x;
            if (y <= z)
            {
                first = y;
                third = z;
            }
        }
        else
        {
            third = x;
            if ( y <= z)
            {
                first = y;
                second = z;
            }
            else
            {
                second = y;
                first = z;
            }
        }
    }
    cout<<first<<second<<third;
}

```

Answer: 
Input/output
------------

You need to output a space between the numbers, otherwise they will look like one long number. It's also customary to finish the output with a newline.

You should verify that you actually received three inputs successfully.

Lint
----

`using namespace std;` is considered a bad habit.

Your code would be more readable if you put some spaces with your operators. For example, `cin>>x>>y>>z;` would look better as `std::cin >> x >> y >> z;`

Only initialize what you need to initialize. For example, `int x, y, z;` is enough — in fact it's safer, because the compiler can then warn you if you subsequently use one of those variables without assigning a value.

Strategy
--------

You've basically manually written out all the possible comparisons that a sorting algorithm would do, the long way. I suggest the following short solution (which doesn't generalize beyond three numbers).

```
#include <algorithm>
#include <iostream>

int main()
{
    int x, y, z;
    std::cout << "Enter three integers to be arranged from least to greatest.\n";
    if (!(std::cin >> x >> y >> z))
    {
        std::cerr << "Error in input.\n";
        return 1;
    }

    int first = std::min({x, y, z});
    int third = std::max({x, y, z});

    // Toss x, y, z into a bag, and take out the min and max.
    // What's left is the middle number.
    int second = x ^ y ^ z ^ first ^ third;

    std::cout << first << ' ' << second << ' ' << third << std::endl;
}

```

Note that `std::min({x, y, z})` uses C++11. For older compilers, you can use `std::min(x, std::min(y, z))`.
**Namespace**

`using namespace std;` [is considered bad practice](http://http%20://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Short code is not a requirement in C++; clear code is preferred.

**Simplify your code**

Since you are swapping two variables multiple times in the code, it would be nice if you made a function call:

```
swap(int& a, int& b)

```

**Extra variables** 

No need for those first, second, third. It can be done by trick of chain of XOR the target variables twice `a ^= b ^= a ^= b;` to swap them.

Here's the finished code:

```
#include <iostream>
#include <utility>

int main()
{
    int x = 0;
    int y = 0;
    int z = 0;

    std::cout << "Enter three integers to be arranged from least to greatest.\n";
    std::cin >> x >> y >> z;

    if (x > y)  std::swap(x, y);

    if (y > z)  std::swap(y, z);

    if (x > y)  std::swap(x, y);

    std::cout << x << ' ' << y << ' ' << z << '\n';
}

```

My original cryptic XOR swap function, before @5gon12eder suggested using `std::swap`:

```
void swap(int& a, int& b)
{
    a ^= b;
    b ^= a;
    a ^= b;
}

```