Need help of a comparison sentence!

Dear all, there are several comparison sentences in my program that probably cause serious problem
1
2
3
4
5
6
7
8
9
10
11
12
if (arrayS[0][i]<=1.25)
{
	arrayf[0][i]=4*arrayS[0][i]-4;
}
if (1.25<arrayS[0][i]<1.75)
{
	arrayf[0][i]=-4*arrayS[0][i]+6;
}
if (1.75<=arrayS[0][i])
{
	arrayf[0][i]=4*arrayS[0][i]-8;
}


Especially the sentence if (1.25<arrayS[0][i]<1.75). There shows a warning of unsafe use of type bool.

Does anyone know how to fix this?
Last edited on
1.25<arrayS[0][i] this returns a boolean value. Then you try to compare if the boolean value is less than 1.75. You can do that, because true will be treated as 1 and false as 0, but it's probably not what you want. Instead you will have to make two separate comparisons combined with the && operator.
if (1.25 < arrayS[0][i] && arrayS[0][i] < 1.75)
Last edited on
This works! thank you for your help!
Note you can avoid that && crap by just using else. less typing, less redundant code, more clarity:

1
2
3
4
5
6
7
8
9
10
11
12
if (arrayS[0][i]<=1.25)
{
	arrayf[0][i]=4*arrayS[0][i]-4;
}
else if (arrayS[0][i]<1.75)
{
	arrayf[0][i]=-4*arrayS[0][i]+6;
}
else
{
	arrayf[0][i]=4*arrayS[0][i]-8;
} 


Topic archived. No new replies allowed.