why can i type-- if(a>5)... instead of if(10>a>5)

J

jienweiwu

The following is correct...
#include <iostream>
using namespace std;
int main()
{
double BMI,height,weight;

cout<<"Welcome to the BMI calculator!\n\n";

cout<<"Enter your weight(kilos): ";
cin>>weight;

cout<<"Enter your height(meters): ";
cin>>height;

BMI = weight/height/height;
cout<<"BMI: "<<BMI<<endl ;

if ( BMI > 34.9)
cout<<"é‡åº¦è‚¥èƒ–";
else
if (BMI>29.9)
cout<<"中度肥胖";
else
if (BMI>26.9)
cout<<"輕度肥胖";
else
if (BMI>23.9)
cout<<"éŽé‡";
else
if (BMI>=18.5)
cout<<"é©ä¸­";
else
cout<<"éŽè¼•";

system("pause");

return 0;

}
The following is wrong...
#include <iostream>
using namespace std;
int main()
{
double BMI,height,weight;

cout<<"Welcome to the BMI calculator!\n\n";

cout<<"Enter your weight(kilos): ";
cin>>weight;

cout<<"Enter your height(meters): ";
cin>>height;

BMI = weight/height/height;
cout<<"BMI: "<<BMI<<endl ;

if ( BMI > 34.9)
cout<<"é‡åº¦è‚¥èƒ–";
else
if (34.9>=BMI>29.9)
cout<<"中度肥胖";
else
if (29.9>=BMI>26.9)
cout<<"輕度肥胖";
else
if (26.9>=BMI>23.9)
cout<<"éŽé‡";
else
if (23.9>=BMI>=18.5)
cout<<"é©ä¸­";
else
cout<<"éŽè¼•";

system("pause");

return 0;

}
BUT WHY CAN'T THE SECOND ONE FUNCTION??
 
B

Bart

The following is correct...
#include <iostream>
using namespace std;
int main()
{
double BMI,height,weight;

cout<<"Welcome to the BMI calculator!\n\n";

cout<<"Enter your weight(kilos): ";
cin>>weight;

cout<<"Enter your height(meters): ";
cin>>height;

Better check your input.
BMI = weight/height/height;

Better avoid code where operator associativity can make things
confusing.

BMI = weight / (height * height);
cout<<"é‡åº¦è‚¥èƒ–";

Better avoid confusing users by prompting them in English and giving an
answer in Chineese.
if ( BMI > 34.9)
cout<<"é‡åº¦è‚¥èƒ–";
else
if (34.9>=BMI>29.9)
cout<<"中度肥胖";

You can write if (BMI <= 34.9 && BMI > 29.9)

But since this is in an else clause you don't need to recheck the first
condition and it is enough to write if (BMI > 29.9)

Also it is customary to indent long else-if lists like this:

if(...)
...
else if(...)
...
else if(...)
...
else
...

Regards,
Bart.
 
F

F.J.K.

if (26.9>=BMI>23.9)
BUT WHY CAN'T THE SECOND ONE FUNCTION??

Because boolean operators are secondary operators, not ternary
operators.

So you wrote the equivalent of:

bool condition1 = (26.9>=BMI);
bool condition2 = (static_cast<double>(condition1)>=23.9);
if (condition2) {
//...
//this is unreachable code, because condition1 is either true or
false and will be
//converted to 1.0 resp. 0.0, which is both NOT >=23.9
}

You should really read a good textbook on the language basics, it will
help you safe so much time in the long run. Of course you should also
read advanced textbooks like Scott Meyers "Effective C++", but first
you need to learn some basic syntax for beginners.
 
R

Ron Natalie

}
BUT WHY CAN'T THE SECOND ONE FUNCTION??

C++ relational operators don't work the way you think.

A < B < C

The result of a relational operator is the boolean
value true or false.

The above expression is two relational operators, effectively
bool temp = A < B;
bool answer = temp < C;

So the second part of the expression compares true or false from
the first half with the value of C. Since bool is another
numeric type, this will work if C is a numeric as well, but
gives you something you weren't expecting.
 
J

Jim Langston

The following is correct...
#include <iostream>
using namespace std;
int main()
{
double BMI,height,weight;

cout<<"Welcome to the BMI calculator!\n\n";

cout<<"Enter your weight(kilos): ";
cin>>weight;

cout<<"Enter your height(meters): ";
cin>>height;

BMI = weight/height/height;
cout<<"BMI: "<<BMI<<endl ;

if ( BMI > 34.9)
cout<<"????";
else
if (BMI>29.9)
cout<<"????";
else
if (BMI>26.9)
cout<<"????";
else
if (BMI>23.9)
cout<<"??";
else
if (BMI>=18.5)
cout<<"??";
else
cout<<"??";

system("pause");

return 0;

}
The following is wrong...
#include <iostream>
using namespace std;
int main()
{
double BMI,height,weight;

cout<<"Welcome to the BMI calculator!\n\n";

cout<<"Enter your weight(kilos): ";
cin>>weight;

cout<<"Enter your height(meters): ";
cin>>height;

BMI = weight/height/height;
cout<<"BMI: "<<BMI<<endl ;

if ( BMI > 34.9)
cout<<"????";
else
if (34.9>=BMI>29.9)
cout<<"????";
else
if (29.9>=BMI>26.9)
cout<<"????";
else
if (26.9>=BMI>23.9)
cout<<"??";
else
if (23.9>=BMI>=18.5)
cout<<"??";
else
cout<<"??";

system("pause");

return 0;

}
BUT WHY CAN'T THE SECOND ONE FUNCTION??

Becuase like someone said:

if ( 23.9 >= BMI >= 18.5 )
will become:
if ( ( 23.9 >= BMI) >= 18.5 )
if ( ( true ) >= 18.5 )
oops, can't compare the boolean true to a number.

Again, like others have said, you want to use the && staement.

23.9 >= BMI >= 38.5
is saying, if 23.9 is greater than BMI AND BMI is greater than 38.5 so write
it that way.
if ( 23.9 >= BMI && BMI >= 38.5 )
Now you are comparing two bools.
if ( ( 23.9 >= BMI) && ( BMI >= 38.5) )
if ( (true) && (true ) )
and we can and two boolean values.

Also, as again, as someone has mentined, put your if statement on the same
line as your else so the indenting doesn't go way off.

if ( BMI > 34.9)
cout<<"????";
else if (34.9>=BMI && BMI > 29.9)
cout<<"????";
else if (29.9>=BMI && BMI > 26.9)
cout<<"????";
else if (26.9>=BMI && BMI >23.9)
cout<<"??";
else if (23.9>=BMI && BMI >=18.5)
cout<<"??";
else
cout<<"??";

There, isn't that much easier to read?

Now, given that, your logical statements are a bit off. Because in the
first if you have already asked if BMI is greater than 34.9, the else if
already knows that BMI has to be less than 34.9 so you dont' need to check
for it again. Same with the others.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,483
Latest member
TedDvb6626

Latest Threads

Top