If statements

Hi I'm having some problems with my conditions. No matter what number I type the second condition always prints out. Any help?

This is what I'm working on:

Classification Level and Number of Teeth

Green --- x <= 10
Yellow --- 10 < x <= 200
Orange --- 200 < x <= 1000
Red --- x > 1000


Write a program that asks for the xenomorph species (a string) and the number of teeth. The program should then display the classification level of the xenomorph.

Note: Xenomorph names are based on famous singers or actors that the xenomorph looks like. For example, Cheryl Crow, Ben Afflect, Matt Damon, Lisa Kudrow, etc. Notice that some of the names are composed of two words so you will need to use getline( ) to accept them from the user.



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
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string name;
	double numOfTeeth = 0.0;

	cout << "Please enter Xenomorph name: ";
	getline(cin, name);
	cout << "Please enter the number of teeth: ";
	cin >> numOfTeeth;

	if (numOfTeeth <= 10)
	{
		cout << "\nXenomorph classification: Green" << endl;
	}
	else if (10 < numOfTeeth <= 200)
	{
		cout << "\nXenomorph classification: Yellow " << endl;
	}
	else if (200 < numOfTeeth <= 1000)
	{
		cout << "\nXenomorph classification: Orange " << endl;
	}
	else if (numOfTeeth > 1000)
	{
		cout << "\nXenomorph classification: Red " << endl;
	}
	cout << "Please press ENTER to end program.";
	cin.sync();
	getchar();
	return 0;
}
Last edited on
< and <= are operators. They take two arguments and returns true or false depending on which argument is bigger.

10 < numOfTeeth <= 200 is treated as (10 < numOfTeeth) <= 200

――――――――――――

Lets say numOfTeeth is 5. That means 10 < numOfTeeth will return false.

false <= 200 is treated as 0 <= 200 so the whole expression will return true.

――――――――――――

Now, lets say numOfTeeth is 15. That means 10 < numOfTeeth will return true.

true <= 200 is treated as 1 <= 200 so the whole expression will return true.

――――――――――――

So whatever value you chose the whole expression will always return true. To test two things you should split each test into it's own expression and combine them using the && (AND) operator, like this: 10 < numOfTeeth && numOfTeeth <= 200

The way you are using it inside your code the 10 < numOfTeeth part is not really needed because it's already tested by the previous if statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (numOfTeeth <= 10)
{
	cout << "\nXenomorph classification: Green" << endl;
}
else if (/*10 < */numOfTeeth <= 200)
{
	cout << "\nXenomorph classification: Yellow " << endl;
}
else if (/*200 < */numOfTeeth <= 1000)
{
	cout << "\nXenomorph classification: Orange " << endl;
}
else /*if (numOfTeeth > 1000)*/
{
	cout << "\nXenomorph classification: Red " << endl;
}
Ohh, I didn't know that. I get it now, thank you for explaining things to me. I appreciate your time.
Topic archived. No new replies allowed.