Sign in to follow this  
kingpinzs

help with c++ looop

Recommended Posts

#include <iostream>
#include <stdlib.h>
using namespace std;

struct TSprite
{
    int x, y;
}Sprite;

TSprite Bullet[10];    

int main(int argc, char *argv[])
{
  
  for (int Loop =1; Loop< 10; Loop++)
  {
      Bullet[Loop].x;
      Bullet[Loop].y;
  cout << Bullet[Loop].x <<"\n";
 
  }

  system("PAUSE");	
  return 0;
}


why dosent Bullet[Loop].x; incrse by one and how can I get it to?

Share this post


Link to post
Share on other sites
You didn't actually do anything to Bullet[Loop].x or Bullet[Loop].y, you just increment Loop. So change these two lines (which do nothing):
Bullet[Loop].x;
Bullet[Loop].y;
To:
Bullet[Loop].x++;
Bullet[Loop].y++;

Share this post


Link to post
Share on other sites
Quote:
Bullet[Loop].x;
Bullet[Loop].y;

These statements do nothing; there is no assignment taking place. You need to change their values:

Quote:
Bullet[Loop].x += 1;
Bullet[Loop].y += 1;


Or:

Quote:
Bullet[Loop].x++;
Bullet[Loop].y++;


or any of a number of other similar variants.

Share this post


Link to post
Share on other sites
if Bullet[Loop].x++;

output
1
1
1
1
1
1
1
1
1

if Bullet[Loop].x;
0
0
0
0
0
0
0
0
0


but what I need is Bullet[Loop].x
to have [Loop] to increse by 1
so what I want displayed is
1
2
3
4
5
6
7
8
9

But I cant figure out how to do that.

Share this post


Link to post
Share on other sites
The simple answer is: you increment each item in the array, and then print out that item. If they start at 0, you will always get 1.

If you want to do what you're saying:

for(int loop = 0; loop < 10; ++loop) {
Bullet[Loop].x = loop;
std::cout<<Bullet[Loop].x<<std::endl;
}

Share this post


Link to post
Share on other sites
for(int i = 0; i < 10; ++i)
{
for(int Loop = 0; Loop < 10; ++Loop)
{
++(Bullets[Loop].x);
++(Bullets[Loop].y);
cout << "bullet: " << Loop << " x: " << Bullets[Loop].x;
cout << "\n\t y: " << Bullets[Loop].y << endl;
}
}

Think about it and you'll see why you need two loops, one to iterate through the array of bullets, and one to increment the x and y values 10 times.

Why don't we just assign the value of the bullet array counter to the x and y coordinates? Because we want to enable various limits (you could go up to a limit of 20, or 30, or 100).

Share this post


Link to post
Share on other sites
Wow now that just made my brain hurt.

so now I dont know what I need to do.

Let see if I can explain it better.

Bullet[Loop].x;

I need Bullet[Loop].x to really look like this
Bullet[1].x;
Bullet[2].x;
Bullet[3].x;
Bullet[4].x;
ect
then I will change Bullet[Loop].x; to = Bullet[Loop].x = player.x; at the start then it needs to increse by itself and then when I make another one it needs to start at player.x but take on its own postion.

Share this post


Link to post
Share on other sites
Quote:
Original post by capn_midnight
Quote:
Original post by baddogj
One thing is, im pretty sure loop should start at 0, not 1. I may be wrong.
how can you not be sure about that?

indexing starts at 0


I'm positive it starts at 0, but i didn't want to risk get fired at for somehow giving falso information

Share this post


Link to post
Share on other sites
can some one fix this or tell me what I ma doing wrong




if (wParam==VK_SPACE)
{

bullettest =1;

for (int Loop = 1; Loop<(rate);Loop++)
{// Bullet[Loop].Screenx = Bullet[Loop].Screenx + 50;
Bullet[Loop].Screenx= x;
Bullet[Loop].Screeny = y;
// Loop = rate;
}



//////inlize
for ( int Loop = 1; Loop< 10;Loop++)
{
Bullet[Loop].top = 0;
Bullet[Loop].bottom =64;
Bullet[Loop].right =64;
Bullet[Loop].left =0;
Bullet[Loop].Screenx= x;
Bullet[Loop].Screeny= y;
}


//loop
int test()
{

for (int Loop = 1; Loop < rate; Loop++)
{


Bullet[Loop].Screenx += xspeed;
Bullet[Loop].Screeny += yspeed;


//if(Bullet[Loop].Screenx< 0 || Bullet[Loop].Screenx> mapX ) xspeed *= -1;
//if(Bullet[Loop].Screeny < 0 || Bullet[Loop].Screeny > mapY ) yspeed *= -1;
bltsprite(Bullet[Loop]);
}


}

//blt image
void bltsprite(TSprite Sprite)
{


r.left = Sprite.left;
r.right = Sprite.right;
r.top = Sprite.top;
r.bottom = Sprite.bottom;
lpBackBuffer->BltFast(Sprite.Screenx,Sprite.Screeny,g_pddbullet, &r,
DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT);

}

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
can some one fix this or tell me what I ma doing wrong
*** Source Snippet Removed ***

The first thing that you're doing wrong is not explaining the expected and actual results.

Anyway, the loop index should start at 0:
for (int Loop =0; Loop< 10; Loop++)

I'm not sure what else if anything is wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by KindFred
you're missing an end brace '}' somewhere. not sure what the code is doing, though, so I can't tell you where.
It seems that he selectively copy and paste'd from the source.

Share this post


Link to post
Share on other sites
every time I hit the sapcebar it is suppose to shoot a bullet then when I hit the space bar agin it is suppose to make another one with oust deleting the first one. but all it does is make one and then just put it back were it started from and if it draws a new one I dont know becaues it is in the same spot.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
IT'S HARD TO READ YOUR INTENT FOR TWO REASON:
1) YOU'RE NOT SPECIFIC OR EXPLAINING PROPERLY.
2) YOU'RE GRAMMAR IS REALLY BAD AND MAKES IT MORE AMBIGOUS.

RELAX....

Share this post


Link to post
Share on other sites
Every time I push the spacebar it is suppose to draw a bullet at x and y then just move it across the screen. When I press the spacebar again it is suppose to draw bullet b at x and y then move it across the screen and bullet A is suppose to stay on the screen and keep moving around the screen.

But what it does is make bullet A and move it across the screen and then I hit the spacebar and it draws bullet a back at x and y as far as I can tell. Or it might be deleting it and drawing a new one I just can’t tell.


P.S I had people proof read it before posting it

Share this post


Link to post
Share on other sites
Ok I know lots of people have explained what you need to do but here's my shot at it.

Heres what you need to do in a simple example:


vector<Bullet> Bullets; // Create a vector list of bullets
Bullet Temp // Just a temp of a bullet object to create more bullets

if(Key = SpaceBar)
{
Bullets.push_back(Temp);//Add another bullet to the list
}

for(int i = 0; i < Bullets.size(); i++) //Loop through your bullets
{
Bullets[i].x++; //Move all the exsisting bullets
Blit(Bullets[i]); //Blit all the exsiting bullets
}


Ok so your first miskate is having an array of bullets. You should use a vector list so you can add bullets as they are created and remove them when need be (if you don't know what a vector is there are tons of tutorials and resources online you can easily find) this way you can have more then 10 bullets at a time and it will be easier to manage them in the long run. Also using a vector list makes 100% more sense for what your doing. If the space bar is pressed, create a bullet (add a new one to the vector list) then you loop through the list and increment their x values (all the exsiting bullets will move right) and then blit all of them (that probably shouldn't be in the same loop but for simplicity reasons i put it there). Easy eh!

So:
1. Use a vector list
2. Create new bullets as you need them
3. Loop through the list of bullets and update thier position
4. Loop through the list and blit them all

I hope that helps and if there are any errors or better suggestions anybody has please say so! (i'm kinda new as well)

Share this post


Link to post
Share on other sites

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