a<b<c

Nov 4, 2016 at 2:46pm
Hey guys !
I need a program that asks for 3 numbers, if the first one is smaller then the second and the second is smaller then the third then the program write True and if not it write False.

For some reason...it writes both of the options as true, no matter what numbers i put in.

any idea why?
thanks

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
 #include <iostream>
#include <cmath>

using namespace std;

int main()
{
	
	double a, b, c;

	cout << "Please enter 3 numbers, the program will determine if they are in correct order\n";

		cin >> a >> b >> c;

		if (a < b < c)
			cout << "The numbers are in order\n"; 
		if (a > b < c)
			cout << "The numbers are not in order" << endl;
		if (a > b > c)
			cout << "The numbers are not in order" << endl;
	
		return 0;




}
Nov 4, 2016 at 2:57pm
The operator < takes two operands and returns a bool.
1
2
3
4
5
if ( a < b < c )

// is like you would do:
bool test = (a < b);
if ( test < c )

That is not what you want.

You do have two separate conditions:
1
2
X: a < b
Y: b < c

Do you want to know whether both are true? Whether X and Y are true?
if ( (a < b) and (b < c) )
Nov 4, 2016 at 2:58pm
What do you think the operation
a < b
evaluates as? It may surprise you! Try outputting it with cout.

You are then going to compare this with c.

If you want to test two successive < or > operations use && (AND)

You also need only
if () ...
else ...
not three separate tests.
Nov 4, 2016 at 3:02pm
Thanks guys, it worked.
Topic archived. No new replies allowed.