Thread: How to Cycle Through Structs

  1. #1
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55

    Question 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. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    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. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    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. #4
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    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. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    Quote Originally Posted by Zach L.
    Graphics will be quite difficult without a good grasp of the language.

    Cheers
    I've noticed

  7. #7
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    Quote 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. #8
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    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. #9
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    Oh, I see.

    Quote 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. #10
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    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.
    Last edited by Dante Shamest; 08-27-2005 at 01:58 PM.

  11. #11
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    Okay, I think I get it now. Thanks!

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    '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
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    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. #14
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM