How do I fill variable amount of structs with a loop?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: How do I fill variable amount of structs with a loop?

  1. #1
    Join Date
    Jul 2009
    Posts
    46

    How do I fill variable amount of structs with a loop?

    I'm writing small poker game simulator. Program will ask for number of players on table (from 2-9) and then will create those amount of structs and fill in a name for each struct. Here's how I see the program so far:

    Code:
    struct player {
    	char name[10];
    	string card1;
    	string card2; }
    
    int numberOfPlayers;
    Player *players = new Player[numberOfPlayers];
    
    cout >> "How many players on the table (2-9): " >> endl;
    cin << numberOfPlayers;
    
    (for int i=0; i<numberOfPlayers; i++)
    {
       players[i]->name = somefunctionhere(i);
    }
    How do I do the last part which is assigning a name (I'll have set number of names, like "John", "Matt", etc for a total of 9 names) to the name part of the number of structs I want to create? So if the user enters 4 for number of players, the loop will create 4 structs and assign name "John" to first struct, "Matt" to second struct, "Joe" to third struct, etc. Maybe have an array of strings like below?

    Code:
    char assignname[9][8];
    
    strcpy(assignname[0], "John");
    strcpy(assignname[1], "Matt");
    strcpy(assignname[2], "Joe");
    Thanks.

  2. #2
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: How do I fill variable amount of structs with a loop?

    The big problem here is that you are using char's instead of strings.
    If you want to have a function return a string value, your life will become infinitely times better if you use std::string.
    Why are you using char for name but string for the cards?

    Code:
    struct player {
    	string name;
    	string card1;
    	string card2; }
    Code:
    string somefunctionhere(int i)
    {
    
        string str;
         switch (i)
          {
             case 0:
                  str = "John";
                  break;
    
            case 1:
                  str = "Matt";
                  break;
                  .........
          }
    
          return str;
    }

  3. #3
    Join Date
    Jul 2009
    Posts
    46

    Re: How do I fill variable amount of structs with a loop?

    I had not thought about why I had 'name' as an char array instead of a string. I suppose I'll make that change. The cards are strings because they come from a string vector. Each element has two characters, the first being the card value (2-A) and the second being the suit (hearts, diamonds, etc).

    The switch statement will not do what I want since, if the person enters 4, the program has to assign all 4 names to their respected name variables in their struct. I suppose I could make it like

    Code:
    case 1: player[1]->name = "John"; break;
    case 2: player[1]->name = "John";
                 player[2]->name= "Matt";
    case 3: player[1]->name= "John";
                 player[2]->name="Matt";
                player[3]->name="Joe";
    
    and so on
    however, it seems like there must be a simpler way to do this.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,423

    Re: How do I fill variable amount of structs with a loop?

    Quote Originally Posted by sysop1911 View Post
    The switch statement will not do what I want since, if the person enters 4, the program has to assign all 4 names to their respected name variables in their struct. I suppose I could make it like
    What you are looking for is std::vector. It can then be resized, unlike arrays. In addition, there is no need to call new[] to do this.

    Secondly, just create a loop and populate the vector with the names.
    Code:
    #include <vector>
    std::vector<player> AllPlayers;
    //...
    int numPlayers;
    const char *names[] = {"John", "Matt", "Joe" /*, etc. */};
    
    // Assume that numPlayers has the number of players
    for (int i = 0; i < numPlayers; ++i )
    {
       player thePlayer;
       thePlayer.name = names[i];
       AllPlayers.push_back( thePlayer );
    }
    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jul 2009
    Posts
    46

    Re: How do I fill variable amount of structs with a loop?

    Thanks, Paul.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center