Public Group

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.

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 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 on other sites
One thing is, im pretty sure loop should start at 0, not 1. I may be wrong.

Share on other sites
Quote:
 Original post by baddogjOne 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 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 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 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 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 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 on other sites
Quote:
Original post by capn_midnight
Quote:
 Original post by baddogjOne 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

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• Forum Statistics

• Total Topics
631428
• Total Posts
3000027
×