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.