Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

help with c++ looop

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

#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
Advertisement
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:
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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!