when i run this it crashes :/

Hi, i have an array of pointers to pointers. Its a deck of cards and we've been told in class to do it that way. I have a build_deck() function and a shuffle_deck() function. They both work fine. And now im onto the deal_card() function, and it works but it just prints out the address of what its pointing to. Any help would be greatly appreciated, here is some of the code :). ive tried de referencing the last bit of code in the deal card function but it crashes :/.

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
Deck::Deck( int total_cards )
{
    _total_playing_cards = total_cards;

    _playing_cards = new PlayingCard*[total_cards];

    _deal_next = 0;
}

void Deck::build_deck()
{

    for( int suit = 1; suit <= 4 && _deal_next != 51; suit++ && _deal_next+13)
        {
            for( int fvalue = 1; fvalue <= 13; fvalue++)
                {
                    _playing_cards[_deal_next] = new PlayingCard( fvalue, suit );
                    cout << PlayingCard( fvalue, suit ) << endl;
                }
           
        }

}

void Deck::shuffle_deck()
{

std::random_shuffle(_playing_cards, _playing_cards + 52);
     
}

PlayingCard Deck::deal_card()
{
 cout << _playing_cards[_deal_next] << endl;
}
Last edited on
but it just prints out the address of what its pointing to.

Yeah well, you're not dereferencing the pointer...
Use the Dereference Operator *.
thanks, but ive already tried that, it just crashes when i try to de reference it..
When creating PlayingCard, when are incrementing _deal_next. It looks like it is always 0.
All elements in the array are null except one.
Last edited on
What did you expect the following to do?

suit++ && _deal_next+13
Not change the value of _deal_next. Where is the assignment operator or ++?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
include <iostream>
using namespace std;
int main()
{
int _deal_next(0);

    for( int suit = 1; suit <= 4 && _deal_next != 51; suit++ && _deal_next+13)
        {
            for( int fvalue = 1; fvalue <= 13; fvalue++)
                {
                    cout << "_deal_next: " << _deal_next << endl;
                }

        }
 return 0;
}
$ ./a.out 
_deal_next: 0
_deal_next: 0
_deal_next: 0
_deal_next: 0
_deal_next: 0
_deal_next: 0
_deal_next: 0
_deal_next: 0
_deal_next: 0
_deal_next: 0
_deal_next: 0
_deal_next: 0
_deal_next: 0
_deal_next: 0
_deal_next: 0
_deal_next: 0
I expected it to increment the suit once, then the deal_next counter 13 times for each suit increment. Which would keep the card counter going up. Should i put _deal_next++ inside the second loop?
++suit, deal_next+=13 should work.

i have an array of pointers to pointers. Its a deck of cards and we've been told in class to do it that way
Let me guess. PlayingCard has no default constructor.
¿Why emulate std::vector? ¿why not teach allocators?
Be careful with the leaks.

By the way, you aren't using total_playing_cards
Let me guess. PlayingCard has no default constructor.


here's the default constructor for the PlayingCard.. i hope lol


1
2
3
4
5
6
PlayingCard::PlayingCard( int face_value, int suit )
    {
        _face_value = face_value;
        _suit = suit;

    }
Topic archived. No new replies allowed.