Sign in to follow this  

for loop skipping to the end after first loop

This topic is 1279 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

 Hi,

I have strange problem with a standard for loop..

It should go from -2, to 2 (-2, -1, 0, 1, 2) but when I debug the program and place the red dot inside the loop and look at the "i" value it goes from -2 directly to 2 after first loop. (-2, 2) which is puzzling me why? 

 

here is the loop:

 
for(int i = -2; i < 3; i++)
{
    if(myMap[(y-2) * myMapW + x+i].properties != 0 && myMap[(y-1) * myMapW + x+i].properties != 1)
    {
        tileTemp.sprite = 1;//if(getTileSprite(x+i, y-2) > 2) tileTemp.sprite = rand()%2 + 1;
	tileTemp.entity = this->getTileEntity(x+i, y-2);
	tileTemp.properties = 2;
	myMap[(y-2) * myMapW + x+i] = tileTemp;
    }
}


I tried to change the negative numbers so the loop should go from 0, 1, 2, 3 and 4 but it goes from 0 to 4 right after first loop... why? 

for(int i = 0; i < 5; i++)
{
    if(myMap[(y-2) * myMapW + x+i-2].properties != 0 && myMap[(y-1) * myMapW + x+i-2].properties != 1)
    {
        tileTemp.sprite = 1;//if(getTileSprite(x+i-2, y-2) > 2) tileTemp.sprite = rand()%2 + 1;
        tileTemp.entity = this->getTileEntity(x+i-2, y-2);
        tileTemp.properties = 2;
        myMap[(y-2) * myMapW + x+i-2] = tileTemp;
    }
}

I use visual studio 2010 and for loop so far have worked as it should.. well, until now

Edited by Killerko

Share this post


Link to post
Share on other sites

yes thats it.. thanks phil_t! there is a mistake in the second part of the if statement..

"myMap[(y-1)" should be "myMap[(y-2)" and thats exactly why it turned false for 1-3 (-1,1)  ..

 

thanks! :)

Share this post


Link to post
Share on other sites

SeanMiddleditch: I'm pretty new to this and still learning mostly.. all I can do while debuging is to place these breakpoints and when the program stops I can examine my variables and they values.. then let the program continue and wait for another stop to examine values again.

How do you use those data breakpoints? that sound like something that could be very useful. 

Share this post


Link to post
Share on other sites

Thank you Lactose, all this is very helpful indeed!

 

I've seen those step over and step into icons on the debugging toolbar in my visual studio but had no idea how to use them so I've never used them before.

Share this post


Link to post
Share on other sites

This topic is 1279 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this