### sorting integers in ascending order

So for my first year programming class, we need to write a little program that gets 3 integers in the main function, and then uses another function to sort them in to ascending order, and then use the main function to print them again. I'm sure there's some little dinky thing I'm missing, but I just can't seem to find what the problem is. compiling doesn't give me any errors, but the console only displays the first integer put in, regardless of order, and does nothing with the other 2.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include #include //load dem libraries using namespace std; double sort3 (double first, double second, double third); //declare the sort function double first; double second; double third; //declare variables int main() { cout << "Hello, please input three integers" << endl; //ask for 3 integers to be sorted cin >> first, second, third; //input them double order = sort3 (first, second, third); //assign the returned value to a useable value cout << "The order is " << narf; //output the order getch(); //do not killself } double sort3 (double first, double second, double third) //the sort function { if (first <= second && second <= third) //these are all conditions to see what the order of the integers will be { return first, second, third; //123 } else if (first <= third && third <= second) { return first, third, second; //132 } else if (second <= first && first <= third) { return second, first, third; //213 } else if (second <= third && third <= first) { return second, third, first; //231 } else if (third <= first && first <= second) { return third, first, second; //312 } else if (third <= second && second <= first) { return third, second, first; //321 } }``````
a function can only have one return value. When you write `return first, second, third;` this is actually the same as `return third;`
okay, so could I do something like...

declare an order variable, and then

 ``1234`` ``````if (first <= second && second <= third) { order = first, second, third; //123 }``````

the rest of the comparisons....
 `` `` ``return order;``

yes?
A variable can only hold one value. You should read into arrays.
Well I know how to do arrays, but at this point in the class we haven't been taught them so I was under the impression we were supposed to figure out how it's done without using them. But I guess doing it wrong is better than it not working. Thanks
what in particular is wrong with this code? I've written programs that have 'return x' in the same function, so long as they are not in the same scope, so that shouldn't be the issue. But the printed text always reads "0the order is: 0" ... do I need to write the program over from scratch, replacing 'double first, second, third' with an array? or is there a quick fix I'm missing? I think the problem is that 'get' is a double, and 'order' is an array, so it can't print out all of the variables, but I can't make 'get' an array because it cant' convert an array to a double (as in sort3)... what to do?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` ``````#include #include //load dem libraries using namespace std; double sort3 (double first, double second, double third); //declare the sort function double first; double second; double third; //declare variables int order[3]; int main() { cout << "Hello, please input three integers" << endl; //ask for 3 integers to be sorted cin >> first, second, third; //input them double get = sort3 (first, second, third); //assign the returned value to a useable value cout << "The order is " <
Not the best solution and not sure if it's allowed, but you can make `first`, `second` and `third` globals, so you don't need to pass or return them.
they are globals though, aren't they?
One, you can't return an array, and two, a function can only have one return value. You really need to listen to what other people have posted, there's no point in asking for help if you just ignore it.

Also, try a sorting algorithm, it will make your code much simpler and nicer.
Last edited on
Topic archived. No new replies allowed.