do while loop

Oct 2, 2015 at 9:24am
hey guys i have a problem with my code am trying to use a do while to create 5 enemies whithin my vector and i expect it to stop pushing back when m=5...but its not working and its still creating more than 5 enemies endlessly...am using sfml 2.3.1 please help.....thanx in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  int m=0;


        do
        {
            if((Timer1.asSeconds()>= 1))
            {


                clock1.restart();

                class random randoms;
                enemy1.rect.setPosition(randoms.random_function(1000),
                                        randoms.random_function(800));

                enemyArray.push_back(enemy1);


            }
            m++;

        }
        while(m<=5);
Last edited on Oct 2, 2015 at 9:25am
Oct 2, 2015 at 9:49am
Either change int m=0; to int m=1; or while(m<=5); to while(m<5);
Also, you don't need the class when you're instantiating an object.
Last edited on Oct 2, 2015 at 9:50am
Oct 2, 2015 at 9:52am
First to declaration:I'm a newbie and I don't know what your function is.
But, in your case.I reckon it is your if series caused the problem.
Try to instead of this below:ps.apologize to my poor English..
int m = 0;
do{
if(Timer1.asSeconds()>=1){
clock1.restart();
class random randoms;
enemy1.rect.setPositon(randoms.random_function (1000),
randoms.random_function(800));
enemyArray.push_back(enemy1);

m++;//this is the change
}
}while(m<5)//there I think you should reset the edge or you may circle 6loops
Oct 2, 2015 at 10:07am
thanx for the reply the problem is that my vector keeps pushing my enemies forever and i only want a fixed value not specifically 5....
Oct 2, 2015 at 10:14am
saniyakhan69++

why u hacking my post dude
Oct 2, 2015 at 10:25am
closed account (48T7M4Gy)
It's a matter of style but I find this way to do the same as other suggestions comfortable. Depends also on the sequence of operations but that's another thing that may/may not be relevant.

1
2
3
4
5
6
7
8
9
10
11
12
13
int m = 0;

while ( m < 5 )
{
    if((Timer1.asSeconds()>= 1))
    {
        clock1.restart();
        class random randoms;
        enemy1.rect.setPosition( randoms.random_function(1000), randoms.random_function(800) );
        enemyArray.push_back(enemy1);
    }
    m++;
}
Last edited on Oct 2, 2015 at 10:25am
Oct 2, 2015 at 11:16am
still outputing more than 5 is there a way to limit my vector or limit the number of pushbacks
Oct 2, 2015 at 12:20pm
closed account (48T7M4Gy)
still outputing more than 5 is there a way to limit my vector or limit the number of pushbacks

By the sound of it the additional items are being added by other parts of your program outside the while loop.
Last edited on Oct 2, 2015 at 12:21pm
Topic archived. No new replies allowed.