Putting numbers in sequence

hi, I've been trying to get this to work now for 4 hours. I've tried all sorts but it just will not work. my original code that I wrote was simpler but it would not work and I got to this.
any way, my problem is, I can sequence the numbers when they come in order, ie: 4,5,6, the program performs this. But, if I reverse the the numbers 6, 5, 4 the program partially works. I will usually end up with either a zero in my output sequence or a -(insert ridiculous number here). I believe my logic is sound, but please some one point me in the right direction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  int main()
{
    int val1=9;//initiate value integer
    int val2=1;//initiate value integer
    int val3=5;//initiate value integer
    
   cout<<"please enter three integers" <<'\n'; //ask for value
   cin>>val1>>val2>>val3;//user enters value
   
   int low;     //initiate the lowest number
   int mid;     //initiate the middle number----these will be the outputs to screen
   int high;    //initiate the higheste number
   
   if (val1<=val2, val1<=val3)		//this block of if statements finds where val1
   low=val1;			            //should be in the sequence
   else if (val1>=val2, val1<=val3) 
   mid=val1;
   else if (val1>=val2, val1>=val3)
   high=val1;
   
   
   if (val1==low,val2<=val3)		//this block then checks against where val1 is located
   mid=val2, high=val3;				//i.e is it low, mid, high, then allocates the remaining
   else if (val1==low,val2>=val3)	// values according to where they come in sequence
   high=val2, mid=val3;
   else if (val1==mid,val2<=val1)
   low=val2, high=val3;
   else if (val1==mid,val2>=val1)
   high=val2, low=val3;
   else if (val1==high,val2<=val3)
   low=val2, mid=val3;
   else if (val1==high,val2>=val3)
   low=val3, mid=val2;
   
   //print result to screen
   cout<<"your values in ascending order are "<<low<<", "<<mid<<", "<<high<<'\n';
   return 0;
}


Edit: I should mention I am at work and do not have access to an IDE so I am using an online compiler hence why the above code has values initiated rather than the user inputting them. (a work around so to speak)

many thanks

Ren
Last edited on
First of all, the comma operator does not have place in conditions. You need boolean and:
val1 <= val2 && val1 <= val3 (true, if both relations are true).

You don't want the comma in the statements either. Use a brace block:
1
2
3
4
5
if ( val1 == low && val2 <= val3 )
{
  mid  = val2;
  high = val3;
}

The line 22, however, has a fundamental problem. If line 14 was false, then low is still uninitialized and therefore comparing the val1 to low has undefined result.
Ok, so I see. I have made a large error. I will need to re-read the chapter again that I am on.
I have edited my code to reflect what you have shown. But it still gives an incorrect value.
Also,
quote:
"The line 22, however, has a fundamental problem. If line 14 was false, then low is still uninitialized and therefore comparing the val1 to low has undefined result."
Because its an if statement, when it returns false wouldn't it move on to the next line?
Sorry. I really am I new to this.
Line 15 is executed only if condition on line 14 is true. Furthermore, if 14 is true, lines 16-19 are skipped.

In other words, the line 14 determines whether to do line 15 or line 16. if else
Yeah I understand that. But, at line 22, wouldn't the same happen?
As in if val1==low. (if at line 14 was false then this would be false too) and the program would move to the next statement at line 24 and so on until all requirements are met?
Or am I miles off.
Hi, so I got to my IDE. Put in the amended code and it works. code is as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  int main()
{
    int val1;//initiate value integer
    int val2;//initiate value integer
    int val3;//initiate value integer

   cout<<"please enter three integers" <<'\n'; //ask for value
   cin>>val1>>val2>>val3;//user enters value

   int low;     //initiate the lowest number
   int mid;     //initiate the middle number----these will be the outputs to screen
   int high;    //initiate the higheste number

   if (val1<=val2 && val1<=val3)		//this block of if statements finds where val1
   low=val1;			            //should be in the sequence
   else if (val1>=val2 && val1<=val3)
   mid=val1;
   else if (val1>=val2 && val1>=val3)
   high=val1;


   if (val1==low && val2<=val3)		//this block then checks against where val1 is located
   {								//i.e is it low, mid, high, then allocates the remaining
   		mid=val2; 					// values according to where they come in sequence
   		high=val3;
   }
   else if (val1==low&&val2>=val3)
   {
   		high=val2;
   		mid=val3;
   }
   else if (val1==mid&&val2<=val1)
   {
   		low=val2;
   		high=val3;
	}
	else if (val1==mid&&val2>=val1)
   {
   		high=val2;
   		low=val3;
   }
   else if (val1==high&&val2<=val3)
   {
   		low=val2;
   		mid=val3;
   }
   else if (val1==high&&val2>=val3)
   {
   		low=val3;
   		mid=val2;
   }

   //print result to screen
   cout<<"your values in ascending order are "<<low<<", "<<mid<<", "<<high<<'\n';
   return 0;
}


I put it in so you can see the solution.

Thank you keskiverto for your help.

1
2
3
int foo;
int bar = 42;
// is it possible that foo==bar? 

Yes, because foo might have any value.

What you need is nested conditions.
1
2
3
4
5
6
7
8
9
10
11
12
if ( a )
{
  if ( b )
  {
    // a and b are true
  }
  else
  {
    // a is true, b is false
  }
}
else // a is false 
Topic archived. No new replies allowed.