Cant Figure out how to fill cards in deck.

Here is my source code i am trying to write a function (fillAll) to fill all 52 cards in a deck and i would like to use my data struct for the function and make Deck one of my paramaters. But i am at a stand stilol i just cannot figure out this enigma. Any advice would be most appreciated.
Thanks,
Dylan

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
enum suit{HEARTS, CLUBS, DIAMONDS, SPADES};
enum face{TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};
struct Card{
	suit suitType;
	face faceType;
};
typedef Card Deck[4][13];
void fillCard(Card fill){
	cout<<”Enter A Suit (Ex. SPADES): “;
	cin>>fill.suitType;
	cout<<”Enter A Face (Ex. TWO): “;
	cin>>fill.faceType;
}
void fillAll(Deck& fullDeck){
	for(int loop=0;loop<52;loop++){
		for(int i=0;i<4;i++){
			for(int j=0;j<13;j++){
				fullDeck[i][j].suitType=suit;
				fullDeck[i][j].faceType=face;
			}
		}
	}
}
closed account (Dy7SLyTq)
line 7: you misused typedef
thank you for the input and how so?
closed account (Dy7SLyTq)
typdef associates a type with a substitute. you could do typedef Card _card or something like that. what you did was declare Deck of type card*. so essentially what that line does is make it so that deck is the same type as card. just remove the typedef. a good use for typedef: typedef unsigned long long ull //you could use ull and unsigned long long interchangebly in your code now
thank you and as far as my function goes im having trouble filling my array with my enums.
does it look okay or am i just missing something because i feel i am.
closed account (Dy7SLyTq)
>:) simple mistake. you forgot to make line 8 an & variable
thank you that makes alot of sence and fixed my errors and whats in my for loops
1
2
fullDeck[i][j].suitType=suit;
fullDeck[i][j].faceType=face;

it doesnt appear to be filling right when i run my program
Last edited on
@dmkavanaugh1s

Just wondering why the outer loop on line 15? The other loops already do what you want - why multiply the whole thing by 52? That is you have 52 * 4 * 13 = 52 * 52

On lines 18 and 19, what is the value of the variables suit and face? Where do they come from? Can you think of a way to get a sensible value for these into your function? You might need to use different variable names for this purpose.

Can you show your main() function?

Hope this helps a bit.
DTSCode wrote:
typdef associates a type with a substitute.

typedef creates an alias for a type.


DTSCode wrote:
what you did was declare Deck of type card*.

No. What he did was make Deck an alias for Card[4][13]. An array is not a pointer, even though we may treat it as one in many circumstances.


DTSCode wrote:
so essentially what that line does is make it so that deck is the same type as card.

This contradicts what you said previously. A Card is not a pointer. A Deck is not a pointer. A Deck is not a Card.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>

enum suit{ HEARTS, CLUBS, DIAMONDS, SPADES };
enum face{ TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };

const char* const suitStr[4] =
{
    "Hearts",
    "Clubs",
    "Diamonds",
    "Spades"
};

const char* const faceStr[13] =
{
    "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
    "Nine", "Ten", "Jack", "Queen", "King", "Ace"
};

struct Card{
    suit suitType;
    face faceType;
};

std::ostream& operator<<(std::ostream& os, const Card& c)
{
    return os << faceStr[c.faceType] << " of " << suitStr[c.suitType];
}


typedef Card Deck[4][13];

void fillAll(Deck& fullDeck)
{
    for (unsigned i = 0; i < 52; ++i)
    {
        suit s = suit(i / 13) ;
        face f = face(i % 13) ;

        fullDeck[s][f].suitType = s;
        fullDeck[s][f].faceType = f;
    }
}

int main()
{
    Deck deck;
    fillAll(deck);

    for (unsigned i = 0; i < 4; ++i)
        for (unsigned j = 0; j < 13; ++j)
            std::cout << deck[i][j] << '\n';
}


http://ideone.com/9E2hg4

Well it's not exactly the same as yours, but it should give you some help? This will fill a deck of 52 cards and output them to screen.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

int main()
{
    struct deck
    {
        std::string suit[52];
        std::string value[52];
    } card;
    std::string suitlist[4] = {"hearts","spades","clubs","diamonds"};
    std::string vals[13] = {"two","three","four","five","six","seven","eight","nine","ten","jack","queen","king","ace"};
    short int x = 0, y = 0, z = 0;
    while(x < 52)
    {
        card.suit[x] = suitlist[y];
        card.value[x] = vals[z];
        ++x;
        ++z;
        if(x % 13 == 0)
        {
            ++y;
        }
        if(z % 13 == 0)
        {
            z = 0;
        }
    }
    x = 0;
    while(x < 52)
    {
        std::cout << card.value[x] << " of " << card.suit[x] << "\n";
        ++x;
    }
    return 0;
}


Thank you everyone for your input i appreciate it and have a better understanding. Cheers.
Topic archived. No new replies allowed.