manipulate a string into name of a variable?
Hello Everyone. I'm new to programming and I have some issue with my code:
I'm not really sure how that thing is called so it's hard to google it;
But I think human being will understand what I mean:
The while loop is increased by i++ each time. In the commented line I want to express
i = 1 , player1.cards[j] = random;
i = 2 , player2.cards[j] = random;
int i = 1;
while (i <= 4)
cout << "Card of Player " << i << " : ";
int j = 0;
while (j < 13)
random = rand() % 52;
if (cards[random] == NULL)
cards[random] = i;
cout << cardName(random) << " , ";
/* player(i).cards[j] = random; */
/* That's what I'm doubt with */
cout << endl;
I tried to define it or manipulate it as a string but didn't work.
Anyone can help me on this? Thanks a lot!
You could define player as a struct array.
If you create a structure like this one:
And declare "player" as
struct PlayerCards player;
, you can write
player[i].cards[j] = random;
In c++, you can't use a string literal as a variable name unless you write code to do that yourself.
Topic archived. No new replies allowed.