Chang
Chang

Reputation: 61

Displaying weird characters Turbo C++

I have a little trouble in the display of heights for my program. This is what i wrote:

if (0 < LaserX < 161) {
LaserX = LaserX/n;
LaserY = LaserY/n;
sprintf(LaserMID, "%.1f, %.1f", LaserX, LaserY);
ShowCo->Text = LaserMID;
}
else { ShowCo->Text = 0; }

So basically it will show the height value when I have LaserX between 0 to 161, and anything out of that it shows 0. But in my case, when the LaserX value is out of the range, it shows me -NAN instead. What is -NAN?! How do i get rid of it? Please advice, thnx.

Upvotes: 1

Views: 425

Answers (3)

iammilind
iammilind

Reputation: 70038

Below condition is not what you want,

if (0 < LaserX < 161) // evaluated from left thus always true (which is unwanted)

You can change it to,

if (0 < LaserX && LaserX < 161)

It precisely means that LaserX is greater than 0 and less than 161.

Edit: NaN = Not a Number.

Upvotes: 3

Ation
Ation

Reputation: 731

NAN - not a number.

Change condition as in answers above. And problem will disappear.

Upvotes: 0

Artyom
Artyom

Reputation: 31273

0 < LaserX < 161

Is wrong, as it is same as

(0 < LaserX) < 161

Which is always true as (0 < LaserX) goes to 1 or 0

You need

0 < LaserX && LaserX < 161

P.S.: Don't use Turbo C++... It is dead, outdated and should not be used

Upvotes: 3

Related Questions