|
-
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.
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
|