0

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

When i = 1 , player1.cards[j] = random; i = 2 , player2.cards[j] = random;

void cardScramble()
{
  int 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 */
        j++;
      }
    }

    cout << endl;
    i++;
  }
  return;
}

I tried to define it or manipulate it as a string but didn't work. Anyone can help me on this? Thanks a lot!

1
  • You have to create an array of objects to accomplish this. Commented Feb 1, 2013 at 19:05

2 Answers 2

4

You cannot do it the way you are thinking of. Instead, combine player1 and player2 (and any other players) into an array. For example:

Player_Type player[2]; // alternatively std::array<Player_Type,2> player;

Then you can refer to each player using i like this:

player[i].cards[j] = random;

Or, if you want to start i at 1, then just subtract 1 from it:

player[i-1].cards[j] = random;
Sign up to request clarification or add additional context in comments.

Comments

1

You can use as mentioned below. You need a structure with an array of int.

 struct Player
    {
        public
        int[] cards = new int[13];
    }; 

Then your while loop goes something like below

Palyer []player = new Palyer[4];

int random;
        int i = 1;
        while (i <= 4)
        {
            cout << "Card of Player " << i << " : ";
            int j = 0;
            while (j < 13)
            {
                random = rand() % 52;
                if (player[i].cards[random] == NULL)
                {
                    cards[random] = i;
                    cout << cardName(random) << " , ";
                     player[i].cards[j] = random; 
                    /* That's what I'm doubt with */
                    j++;
                }
            }
            i++;
        }

//Like the answer if it is helpful

2 Comments

Is it case sensitive (for the variable "Player")?
Player is struct. Later i have declared an array of struct of Palyer and named as player.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.