-
March 8th, 2011, 10:34 PM
#1
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.
-
March 8th, 2011, 11:31 PM
#2
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;
}
-
March 9th, 2011, 12:39 AM
#3
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.
-
March 9th, 2011, 12:55 AM
#4
Re: How do I fill variable amount of structs with a loop?
Originally Posted by sysop1911
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
-
March 9th, 2011, 11:24 PM
#5
Re: How do I fill variable amount of structs with a loop?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|