I'm trying to do an exercise from the book :Programming -- Principles and Practice Using C++ (Second Edition)
Write a program that prompts the user to enter three integer values, and then outputs the values in numerical sequence separated by commas. So, if the user enters the values 10 4 6, the output should be 4, 6, 10. If two values are the same, they should just be ordered together. So, the input 4 5 4 should give 4, 4, 5.
This is what i did :
#include "std_lib_facilities.h"
int main(){
int val1 = 0;
int val2 = 0;
int val3 = 0;
cout << "input 3 integers values with spaces between them\n";
cin >> val1 >> val2 >> val3;
int bigger = 0;
int middle = 0;
int smallest = 0;
if(val1 >= val2 && val1 >= val3) {
bigger = val1;
}
if (val1 <= val2 && val1 <= val3) {
smallest = val1;
}
else {
middle = val1;
}
if(val2 >= val1 && val2 >= val3) {
bigger = val2;
}
if (val2 <= val1 && val2 <= val3) {
smallest = val2;
}
else {
middle = val2;
}
if(val3 >= val1 && val3 >= val2) {
bigger = val3;
}
if (val3 <= val1 && val3 <= val2) {
smallest = val3;
}
else {
middle = val3;
}
cout << smallest << " " << middle << " " << bigger << '\n';
return 0;
}
The program work for some values but not all i.e : when val3 is bigger than val1 and val2, i think that there is a problem at the end : else{ middle = val3; }
The correction of Strousput : http://stroustrup.com/Programming/Solutions/Ch3/e3-6.cpp
I'm sorry i didn't put any comments !