I am sorry for my bad English I will try my best so you can understand me :) I am studying C++ from Stroustrup's book and I'm trying to do this excersise with sorting 3 numbers using if-statements. I think I've done pretty good job writing this code, at least I feel so. I have done some calculations on paper so I could understand it better, and I came up with this code:
#include <iostream>
int main()
{
int val1;
int val2;
int val3;
std::cout << "Please enter the 1st number: ";
std::cin >> val1;
std::cout << "Please enter the 2nd number: ";
std::cin >> val2;
std::cout << "Please enter the 3rd number: ";
std::cin >> val3;
if (val1 > val2)
{
if (val2 > val3)
{
std::cout << val3 << ' ' << val2 << ' ' << val1;
}
else
{
if (val1 > val3)
std::cout << val2 << ' ' << val3 << ' ' << val1;
else
std::cout << val2 << ' ' << val1 << ' ' << val3;
}
}
else
{
if (val2 > val3)
{
if (val3 > val1) std::cout << val1 << val3 << val2;
else std::cout << val3 << val1 << val2;
}
else
{
std::cout << val1 << val2 << val3;
}
}
My question is: Is there anything I could do to improve this code? I've tried pretty much everything I could, but I can't get it to have less if-statements, I don't know if it's the least if's I can get with this exercise, please help me out :)