• Advertisement
Sign in to follow this  

Why will this if statement not work?

This topic is 4282 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Once again ran into a problem that I can't figure out.. after I got the last one solved, I've been sitting here trying to figure this one out. Does anything look wrong with this function to you?
bool block::atBottom()
{
    if(x <= -0.9)
    {
        moveable = false;
        return true;
    }
    else
        return false;
}


I put in error checking message boxes before and after the if(x <= -0.9) it shows up before, but not inside the brackets. Why will this inequality not work? P.S. x is part of the block class, and so is moveable, if that variable matters in this situation, which it shouldn't. P.P.S It's probably something I'm going to feel stupid for, but hey, its 2:00 AM here.

Share this post


Link to post
Share on other sites
Advertisement
I see nothing wrong in that code. Maybe you should try to print out the value of 'x' and see what it is set to.

Share this post


Link to post
Share on other sites
The <= makes me think you're counting on equality, which is a No-No when you're working with floating point numbers.

float a = 0.9 / 3.0;

if(a <= 0.3)
{
// This may not execute!
}

Put simply, to a computer, 0.9/3.0 may equal 0.9000000000243, even if it prints out as 0.9. For more, read this.

Share this post


Link to post
Share on other sites
I can't figure out how to print it out, it won't let me convert it from float to const char*, so I can pass it through MessageBox(). Hmmmm.

Share this post


Link to post
Share on other sites
If that is the case, then how can I tell my blocks when to stop on the board, when they reach the bottom?

Share this post


Link to post
Share on other sites
Quote:
Original post by Niddles
I can't figure out how to print it out, it won't let me convert it from float to const char*, so I can pass it through MessageBox(). Hmmmm.


Try formatting your message with sprintf().

Share this post


Link to post
Share on other sites
The atBottom function is dealing with x.
You sure its not supposed to be y?

Just a thought...

Share this post


Link to post
Share on other sites
I can't get debug to work in dev cpp
It says my project doesn't have debug information, do you want to add? I click yes, and then go to debug, and it goes through the same thing.

Share this post


Link to post
Share on other sites
Post the rest of the code. The function looks OK, but we can't have any idea why it doesn't do what you want it to do without knowing how you use it in the context of the program.

Share this post


Link to post
Share on other sites
Hey heres an idea. You changed your x from the original code to y correct. So your code looks something like this now:


bool block::atBottom()
{
if(y <= -0.9)
{
moveable = false;
return true;
}
else
return false;
}




By the looks of the function it looks like your trying to find out if the block is at the bottom. So why are you using <= if the blocks are falling. And if your dealing with 3D here then -0.9 is just below the origin right. So your going to have to have your blocks below the origin and have them travel up the y-axis. This might solve the problem its how i would do it:


bool block::atBottom()
{
if(y >= -0.9) // If the y coordinate is below -0.9
{
moveable = false;
return true;
}
else
{
return false;
}
}




Just make sure the blocks start above -0.9 and that they are falling downwards. Good-luck and i hope i helped out a little.

Share this post


Link to post
Share on other sites
Quote:
Original post by Niddles
I can't get debug to work in dev cpp
It says my project doesn't have debug information, do you want to add? I click yes, and then go to debug, and it goes through the same thing.

You need to make sure that you are building your project in 'debug' mode. If that fails, try searching around the build options to see if there is an option to include debug information in your project.

Concerning your function: -



bool block::atBottom()
{
if(x <= -0.9)
{
moveable = false;
return true;
}
else
return false;
}




would you not want to reset the 'moveable' variable if x > -0.9?

AKA



bool block::atBottom()
{
if(x <= -0.9)
{
moveable = false;
return true;
}
else
{
moveable = true;
return false;
}
}


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I would say drop this
if(x <

Share this post


Link to post
Share on other sites
At what y value do the blocks start falling, and what is the y value of a block at the bottom?

| | <--- start value == ?
| |
| |
| |
| |
| |
| |
| |
| |
| | <--- bottom value == ?
+----------+

Without knowing that we can only guess about the semantics of your code.

BTW your (apparently incorrect) code can be abbreviated to one line (with the same incorrect behaviour):

bool block::atBottom()
{
return (!(moveable = (!(x <= -0.9))));
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why not use the 'f'-postfix?
(From an example above):
flaot a = 0.9/3.0;

if(a <

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

float a = 0.9/3.0;

if(a <= 0.3f)
{
//should execute
}

Share this post


Link to post
Share on other sites

bool block::isAtBottom() { return (x <= -0.9f); }


you should separate logic from conditions, moveable = false; is more something what results when block is at bottom, rather not the condition.

double and floating point are different. I wouldn't be surprized if your first try would work in another compiler thought.

Share this post


Link to post
Share on other sites
Because I have never zoomed out in OpenGL, the top of the screen is 1.0f, and the bottome is 1.0f. The blocks start falling at 0.76. I want the if statement to read if y is <= to -0.9 so if the block gets to the bottom and the y of the block is -0.9, or if it miraculously gets past -0.9 it will stop.

Share this post


Link to post
Share on other sites
Here I will post what code will deal with the atBottom function.

//in game.cpp
//Game loop
void gameRun(HDC hDC)
{

glClearColor(0.0f, 255.0f, 255.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();

if(GameBoard.currBlockPos == STARTING)
{
GameBoard.currblock = GameBoard.nextblock;
GameBoard.nextblock = new block(false);
GameBoard.currBlockPos = FALLING;
}
if(GameBoard.currBlockPos == FALLING)
{
if(!GameBoard.currblock->atBottom())
{
GameBoard.currblock->moveBlockDown();
}
else
{
GameBoard.currBlockPos = STARTING;
}
}
GameBoard.drawBack();
glPopMatrix();
SwapBuffers(hDC);
Sleep(1);
}



currblock is a pointer to a block declared in GameBoard, currBlockPos is an enumeration telling what the current block is doing, Starting or Falling. GameBoard.drawBack is a GameBoard function drawing the background

//The function in block.cpp
bool block::atBottom()
{
if(y <= -0.9)
{
moveable = false;
return true;
}
else
return false;
}


Share this post


Link to post
Share on other sites
I figured it out!

int starting = 1;
if(GameBoard.currBlockPos == STARTING)
{
if(starting == 1)
{
GameBoard.currblock = new block(true);
starting++;
}
else
{
GameBoard.currblock = GameBoard.nextblock;
}
GameBoard.nextblock = new block(false);
GameBoard.currBlockPos = FALLING;
}

When it was starting earlier, it was trying to pull information out of a NULL pointer, because I was assigning currblock to equal nextblock, but nextblock wasn't defined yet.

Share this post


Link to post
Share on other sites
Okay well, Windows doesn't give me an error report, but the screen is coming up black, after I'm clearly clearing to cyan, and drawing to the background.. Hmmm

Share this post


Link to post
Share on other sites
Quote:
BTW your (apparently incorrect) code can be abbreviated to one line (with the same incorrect behaviour):

bool block::atBottom()
{
return (!(moveable = (!(x <= -0.9))));
}



PROVED!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement