ID:140789
 
Code:
Alright, I'm working on a C++ program for my programming class, it's pretty basic(Since we're just starting) but I can't get it to work right.
#include "std_lib_facilities.h"


int main()
{
int num1, num2, num3;
int first = 0, second = 0, third = 0;
cout << "Enter 3 numbers(Seperate them with space.)\n";
while(cin >> num1 >> num2 >> num3){
if(num1 >= num2 && num1 >= num3)
first = num1;
else if(num2 >= num1 && num2 >= num3)
first = num2;
else
first = num3;

if(num1 <= first && num1 >= num2 && first != num2 || num1 <=first && num1 >= num3 && first != num3 )
second = num1;
else if(num2 <= first && num2 >= num1 && first != num1 || num2 <=first && num2 >= num3 && first != num3 )
second = num2;
else if(num3 <= first && num3 >= num2 && first != num2 || num3 <=first && num3 >= num1 && first != num1)
second = num3;

if(num1 <= num2 && num1 <= num3)
third = num1;
else if(num2 <= num1 && num2 <= num3)
third = num2;
else if(num3 <= num1 && num3 <= num2)
third = num3;
cout << first << ", " << second << ", " << third << '\n';
first = 0;
second = 0;
third = 0;
num1 = 0;
num2 = 0;
num3 = 0;
}

keep_window_open();



Problem description:
I know it's a bit bad and inefficient, but this is all we "know" in C++ so I don't want to overstep my boundaries(Plus I couldn't think of any other way to do it >_>). The program works in every instance except one. When the numbers are ordered like this 3 1 2 it'll out put 3, 3, 1 and I can't seem to find the problem, any suggestions?

edit
The program is supposed to order numbers from greatest to least. If there are any other instances of it not working please let me know.
I can't even follow that logic. Your if() statements should be nested like so:

if()
if()
//case 1
else if()
//case 2
else
//case 3
else
if()
//case 4
else if()
//case 5
else
//case 6


Each if() statement only needs to make one comparison.
Seriously, this is a mess, and it should've been clear to you too. As Garthor said, you need to refactor the whole logic in there so it actually makes sense and is readable without completely dissecting it. Then you can start looking for the problem (if it's even still there).
Also, non-DM-related code problems should go in Computers & Technology (which is in the off-topic section). It also makes sense to mention the name of the language in the topic line.
I just modified it.. (I'm not that familiar with C++)
#include "std_lib_facilities.h"

int main()
{
int num1, num2, num3;
int tmp;
cout << "Enter 3 numbers(Seperate them with space.)\n";
while(cin >> num1 >> num2 >> num3)
{
if(num1 < num2)
{
tmp = num1;
num1 = num2;
num2 = tmp;
}
if(num1 < num3)
{
tmp = num1;
num1 = num3;
num3 = tmp;
}
if(num2 < num3)
{
tmp = num2;
num2 = num3;
num3 = tmp;
}
cout << num1 << ", " << num2 << ", " << num3 << '\n';
tmp = 0;
num1 = 0;
num2 = 0;
num3 = 0;
}
keep_window_open();
I guess this is a bump, but I figured out the problem and completely re-programmed the whole thing.
#include "std_lib_facilities.h" // include the library facilities
int max(int num1, int num2) // returns the greater value
{
if(num1 >= num2)
return num1;
else return num2;
}

int min(int num1, int num2) // returns the lesser value
{
if(num1 <= num2)
return num1;
else return num2;
}
int middle(int greater_two_num, int less_two_num, int num1, int num2, int num3)
{
// returns the number that is greater than less_two_num and less than greater_two_num
// integer 1 check
if( num1 <= (greater_two_num, num2) && num1 > min(less_two_num, num2) )
return num1;
else if( num1 <= (greater_two_num, num3) && num1 > min(less_two_num, num3) )
return num1;
// integer 2 check
if( num2 <= (greater_two_num, num1) && num2 > min(less_two_num, num1) )
return num2;
else if( num2 <= (greater_two_num, num3) && num2 > min(less_two_num, num3) )
return num2;
// integer 3 check
if( num3 <= (greater_two_num, num1) && num3 > min(less_two_num, num1) )
return num3;
else if( num3 <= (greater_two_num, num1) && num3 > min(less_two_num, num1) )
return num3;
}

int main()
{
int num1, num2, num3; //entered numbers
int greater_two_num = 0; // greater of the first two numbers inputed.
int less_two_num = 0; // lesser value of the first two numbers inputed.
int first = 0; // first number.
cout << "Enter 3 integers(Seperated with space.)\n";
cin >> num1 >> num2 >> num3; // input numbers

greater_two_num = max(num1, num2); // generate the bigger of 2 numbers
first = max(greater_two_num, num3); // generate the first(biggest) number
less_two_num = min(num1, num2); // generate the lesser of the 2 numbers

cout << first << ", " << middle(greater_two_num, less_two_num, num1, num2, num3) << ", " << min(less_two_num, num3) << '\n';
keep_window_open();

}
In response to Axerob
I fail to see how you need int middle().

You have the highest and lowest value, that's all you need.

Here's some pseudo-dm-code I just made up for you, to show my point.

var
num1
num2
num3
max
min
middle
proc/MAX(list/args)
var/max = args[1]
for(var/A in args)
if(max < args[A]) max = args[A]
return max
proc/MIN(list/args)
var/min = args[1]
for(var/A in args)
if(min > args[A]) min = args[A]
return min

min = MIN(list(num1,num2,num3))
max = MAX(list(num1,num2,num3))

switch(min+max)
if(num1+num2) middle = num3
if(num1+num3) middle = num2
if(num2+num3) middle = num1
In response to Andre-g1
Because max() and min() only have 2 arguments, so it's only checking two numbers, not 3. For some odd reason I get some really weird error whenever put 3 arguments with either function.