If statement error

This If statement isn't working, even though the immediate box says that food[c].exists==true, why?

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
bool exists[100];
for(int c=0;c<CIRCLES;c++)
}//LOOP for
for(int c=0;c<FOOD_NUM;c++)
}//end if
}//LOOP for
EndPaint(hWnd, &ps);

just let me know if you need any more of the program.
Last edited on
Possible causes:

1) food[c].exists might be false (how are you confirming it is true?)
2) the if statement actually is working, but your Rectangle() line is broken and isn't drawing anything (or is drawing it offscreen).

To further diagnose:

1) Put a breakpoint on the if statment, and on the following Rectangle() line and confirm the breakpoints trip when you expect them to.
2) While in the debugger, watch food[c].exists to make sure it contains a value you expect
3) If neither of the above reveal your problem, examine all food[c] members to make sure you're drawing the rectangle on screen.
here's the thing: I've already used breakpoints to confirm that the rectangle line is never executed, also, according to a watch, food[c].exists is true when it encounters the if statement. the only odd thing about food[c]'s member variables is that it's xpos and ypos always contain apparent garbage, even though I specifically set the entire array to random values of x and y on the screen

here is WM_CREATE

class Food
int xpos,ypos;//position
bool exists;//should it be painted or taken account of for collision detection?

void Generate_Food(int xpos, int ypos)//for initial creation and regeneration, but needs some work; apparently
Food::exists=true;//makes food exist
Food::xpos=xpos;//sets position
Food::ypos=ypos;//also sets position
};//END Generate_Food
void Terminate_Food()//when eaten
};//END Terminate_Food

bool Paint_Food(int xpos, int ypos, bool existence)//for constant repainting
if(Food::exists==true)//tests whether food should be painted or not
return true;
return false;
};//END Paint_Food


Food food[100];
for(int i=0;i<100;i++)
return 0;
If I'm understanding this correctly... you have a scope problem. I'm surprised this was compiled without errors.

Your 'food' array is declared as a local variable inside your WM_CREATE handler. Local variables go out of scope and die at the next closing brace (for you, it seems this would be the end of the switch statement).

So yeah... you are initializing your 'food' array in a WM_CREATE message. But then as soon as WM_CREATE finishes, that entire array gets thrown away. Then when you get a WM_PAINT message, an entirely new 'food' array is made (but not initialized!)

The compiler really should be throwing an error about this. Typically the error comes in the form "construction of food skipped by case label" or something along those lines. I don't know how this slipped by it.

The solution here is to move your 'food' array so it has a broader scope and survives past the end of this message handling function. The quick and dirty way to do this is to make it global.

On a side note, please put your code in code tags:

Paste your code between these tags
It was in fact a scope error, and actually there were several others in my code, so thank you so much for pointing that out!

I also have some trouble believing that my compiler did not catch this, seeing as Visual C++ is supposedly top notch, but at least you found it!
What version of Visual C++ is this? Mine catches these errors.
2010 Express Edition. Perhaps if I had the full version. Ah well..
nah, I'm also using VS 2010 express. Weird.
If you are at all interested in helping with this simulation further, I hit a plethora of bugs with the Bacteria class, look at the post "Very buggy simulation". If this counts as spam, sorry, I'm new to the site. Also I think my VS 2010 express might be a little weird, I mess with my settings all the time!
Topic archived. No new replies allowed.