loop fall through issue

for my tutorial I have got to write a program that shows the difference between 2 ages and acts differently if one of the ages is over 100. i managed to get a partial result but why does it fall through the loop and still say too high even if both ages are under 100.
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
28
29
30
31
32
  #include<iostream>

using namespace std;

int main()
{
	int a,b;
	
	cout << "Enter an age under 100\n";
	cin >> a;
	
	cout <<"Enter another age under 100\n";
	cin >> b;
	
	if
	(a < 100 && b < 100 && a < b)
	{
	cout <<"Age 1 was less than age 2\n";
	}
	else if(a < 100 && b < 100 && b < a)
	{
		cout << "Age 2 was less than age 1\n";
	}
		else ( a > b || b > a && a > 100 || b > 100 );
		{
			cout << "To high\n";
		}
		
}
	
	
	
why does it fall through the loop

You have no loop.

Line 24: You have some problems in the statement.
1) You can't put a condition on an else clause.
2) The ; terminates the if/else statement. Line 26 is executed unconditionally.
3) You're ignoring operator precedence.
http://www.cplusplus.com/doc/tutorial/operators/
The condition is going to be evaluated as:
 
( a > b || (b > a && a > 100) || b > 100 )

Note the extra parens.

BTW, what happens if the ages are the same?

Last edited on
Registered users can post here. Sign in or register to post.