# Thread: How to Cycle Through Structs

1. ## How to Cycle Through Structs

I'm very new to these forums and C++, so I will have some very basic questions. One of them is: How do you check each struct for something with a for loop? I've done it in other languages, but I don't know the C++ syntax for it. And does anyone know where I can find a very basic C++ graphics tutorial? So far I've just been doing text-based stuff, but I want to see if I can move on to graphics.

2. You're going to have to better explain what you mean by "check each struct for something with a for loop."

C++ doesn't have graphics built in; you need to use a library for that.

3. Code:
```typedef struct
{
int a;
int b;
} stuff;

int main()
{
stuff a[10];
for(int i = 0; i < 10; i++)
{
a[i].a = someNumber;        // Assign
if(a[i].b == somethingElse) // Compare
doThings();
}
return 0;
}```
Is that what you are talking about? The '.' operator is used to refer the struct members, and other than that it's just the same as doing anything else.

4. Oh sorry. Well, I've been using Blitz Basic, which is a lot different than C++ and most other languages in that everything is there for you. Everything is built in to the language, so that's why libraries and things like that are very confusing for me. So your saying I have to use something like DirectX or OpenGL? But, this is how you would do what I'm talking about in Blitz Basic. In Blitz Basic you use Types which are the same thing as structs. Let's say I wanted to print the position of each enemy ship in a game:

For e.enemyship = Each enemyship
Print e\x
print e\y
next

I'm wondering how you would do that in C++. Check each struct's variables or do something with them. I don't know if this is possible in C++, I'm just assuming it is.

5. Well, you'd need an array (or other container) and loop through it as in Tonto's example.

I suggest you start going over the fundmentals of C++ first. Graphics will be quite difficult without a good grasp of the language.

Cheers

6. Originally Posted by Zach L.
Graphics will be quite difficult without a good grasp of the language.

Cheers
I've noticed

7. Originally Posted by Tonto
Code:
```typedef struct
{
int a;
int b;
} stuff;

int main()
{
stuff a[10];
for(int i = 0; i < 10; i++)
{
a[i].a = someNumber;        // Assign
if(a[i].b == somethingElse) // Compare
doThings();
}
return 0;
}```
Is that what you are talking about? The '.' operator is used to refer the struct members, and other than that it's just the same as doing anything else.

Wait, what's typedef? That's different than a structure, right?

8. typedef gives another name, an alias, to an existing type.

Code:
```typedef struct
{
int a;
int b;
} stuff;```
Here typedef gives the anonymous struct a name called stuff.

But why bother with another name? This is because in C, if you had just defined the struct like this

Code:
```struct stuff
{
int a;
int b;
};```
You would then have to create an object of stuff like this.

Code:
`struct stuff stuff1;`
The typedef makes it easier to do it like this.

Code:
`stuff stuff1;`

9. Oh, I see.

Originally Posted by Tonto
Code:
```	stuff a[10];
for(int i = 0; i < 10; i++)
{
a[i].a = someNumber;        // Assign
if(a[i].b == somethingElse) // Compare
doThings();
}```
But then, I don't understand how he's storing this in an array. Where does a come from in the "stuff a[10]" line?

10. Code:
`stuff a[10];`
The code above creates an array of 10 stuff objects.
The name of the array is a.

Code:
`a[ i ].a = someNumber;`
This accesses the a data member of the i-th stuff object in the array a. Maybe he should have chosen a more descriptive name for the array.

11. Okay, I think I get it now. Thanks!

12. 'a' is declared on that line to be an array of structs of type 'stuff' which stores 10 elements. You really might want to start with the tutorials such as the ones on ( www.cprogramming.com )

Also, note that Dante Shamest's comment about typedefing structs applies to C, not C++.

Cheers

13. Okay, so let's say that I had 10 enemy ship structs in an array, but I want to make a new one. How do I add that to the same array?

14. Assuming the array size is 10, you cannot. Use a std::vector instead. Do a board search for vector as well, and you'll get lots of results.

Popular pages Recent additions