help with c++ looop

Started by
18 comments, last by SirSmokey 19 years, 4 months ago

#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?
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++;
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
One thing is, im pretty sure loop should start at 0, not 1. I may be wrong.
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="88" height="70" id="H2lvl" align="middle"> <param name="allowScriptAccess" value="sameDomain" /> <param name="movie" value="http://www.ironhive.com/CFCs/H2lvl.swf?gamertag=jBaddog" /> <param name="quality" value="high" /> <param name="wmode" value="transparent"> <embed src="http://www.ironhive.com/CFCs/H2lvl.swf?gamertag=jBaddog" quality="high" bgcolor="#ffffff" width="88" height="70" name="H2lvl" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> </object>
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

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

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.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
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.
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;}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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).
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.
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
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="88" height="70" id="H2lvl" align="middle"> <param name="allowScriptAccess" value="sameDomain" /> <param name="movie" value="http://www.ironhive.com/CFCs/H2lvl.swf?gamertag=jBaddog" /> <param name="quality" value="high" /> <param name="wmode" value="transparent"> <embed src="http://www.ironhive.com/CFCs/H2lvl.swf?gamertag=jBaddog" quality="high" bgcolor="#ffffff" width="88" height="70" name="H2lvl" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> </object>

This topic is closed to new replies.

Advertisement