Hey everybody, I am trying to initialize a deck of cards using a structured data type. Here is the currently relevant portions of the code:

struct ACard
{
int Num;
char Pic;
};


void InitiateCard(ACard Card[])
{
	for (int i =0; i < 13; i++)
	{
	Card[i].Num = i;
	Card[i].Pic = 3;
	}

	

}

Using the above, I can get a first full suite of cards (0-13, of hearts). However, im not exactly sure how to proceed with the rest of the deck. I know it involves using at least one more For loop but am uncertain how to proceed.

Basically I need 4 more sets of Num 0-13 for the corresponding 4,5, and 6 for the Pic.


Thanks!

You want i to run from 0-51 if i is the index of each card in the array representing a deck of cards. You wand 0-12, to get 13 cards, in each suit. You want each suit to have 13 cards .

for each suit
  for each face value
    assign a face value and suit to each card in the deck.

You could also do something like this

for (int i =0,curr_pic=3; i < 52; i++)
	{
	Card[i].Num = i%13;// Num will only get value from 0-12
	Card[i].Pic = curr_pic;
	if( i%13==0) //after every 13 cards increment the value of cur_pic
		cur_pic++;
	}

Thanks for the quick response. Right now my code looks like this:

for (int i = 0; i < 4; i++)
{
	for (int j = 0; j < 13; j++)
	{
				Card[j].Pic = i;
				Card[j].Num = j;
	}
}

I am still only getting one proper suite. I think I need to somehow tell it to go to the next suite without overwriting the previous ones.

You would be getting only suite 3 right?
The problem is in
Card[j].Pic = i;
Card[j].Num = j;
Tell me: should this be j or something else?

Thanks for the help both of you. I ended up just rewriting the entire thing and doing it in a slightly different way.

int count = 0;
for (int i = 3; i <=6; i ++){
  for (int j = 0; j < 13; j++){
      Card[count].Pic = i;
      Card[count].Num = j + 1;
      ++count;}}

Not very readable.
Here is the fix of the code you posted before the last one:

for (int i = 0; i < 4; i++)
{
	for (int j = 0; j < 13; j++)
	{
				Card[ (i*13) + j ].Pic = i;
				Card[  (i*13) + j].Num = j;
	}
}

You may argue that this code is far less readable. But the fact is that this is more neat.
If you are unsure why is : (i*13) + j you should try to check for some values of i and j

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.