Pointers in an argument
Jan 23, 2012 at 10:58am UTC
in my program, i have a function creating a deck, and a function dealing the cards to 2 different hands in a multi-dimensional array ([2][5]), i am creating a function to print the hands, however, a requirement is to have a pointer in the arguement,i.e. printHands(Card * hands[]), this is what i have so far...
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
void dealCards()
{
int j, i, a;
for (i = 0; i < 2; ++i)
{
for (a = 0; a < 5; ++a)
{
srand (time(NULL));
j = rand() % 52 + 1;
hand[i][a] = c[j];
}
}
}
void printHands(Card * hand[10])
{
for (int j = 1; j < 3; ++j)
{
cout << "hand " << j;
for (int i = 0; i < 5; ++i)
{
hand[x] = &hand[j][i];
switch (hand[x]->getvalue())
{
case 1: cout << "Ace" ; break ;
case 11: cout << "Jack" ; break ;
case 12: cout << "Queen" ; break ;
case 13: cout << "King" ; break ;
default : cout << hand[x]->getvalue(); break ;
}
cout << " of " << hand[x]->getsuit() << endl;
}
}
}
int main ()
{
createDeck(); dealCards();
printHands();
return 0;
}
the 'Card * hands[]' is a requirement, so what arguement do i need to use in line 44 to make it run??
p.s. i included the dealHands function so you can see as to what format they are in
Jan 23, 2012 at 11:04am UTC
Don't think you can pass a [2][5] array as a [10]* pointer.
Passing (MD) arrays by argument is one of the very few things that is actually easier with dynamically allocated arrays. Are you allowed to use those? (i.e. use the "new" keyword)
Jan 23, 2012 at 11:10am UTC
i am allowed to use those, to where were you thinking i add it?
Jan 23, 2012 at 11:54am UTC
anyone else got any input??
Jan 23, 2012 at 12:26pm UTC
anyone?
Jan 23, 2012 at 1:48pm UTC
'Card * hands[]' is a requirement
If you're not allowed to use the STL then..
I think dealCards should have a similar argument
you'd have :
1 2 3 4 5 6 7 8
void createDeck(Card deck[])
{...}
void dealCards(Card deck[], Card * hand[])
{...}
void printHands(Card * hand[])
{...}
Arrays could be replaced by pointers, i.e. you could rewrite dealCards prototype as
void dealCards(Card *deck, Card** hands)
Also, you may have a problem with how you are dealing the cards, you can draw the same one many times
Topic archived. No new replies allowed.