Poker problem (trouble with classes)

Hi,

I am creating a poker program for school (learning classes). The instruction are to make a class CARD and a class DECK with associated files. DECK is to be a vector of cards.

I have created the classes but am having a hard time with understanding how they work together. My main problem seems to be that I am not creating the DECK correctly. In the DECK constructor function I can access & manipulate the vector normally, but all other functions do not work. It seems like the DECK is not being created from CARD class in the proper manner.

When my DECK constructor calls function init() I get an error saying that the vector is out of range. If I move the init() function code into the constructor function, it works fine.


Please let me know where I am going wrong with this. Thanks.


main function looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "Deck.h"
#include "card.h"
using namespace std;

#define MAX 16000


int main ()
{
	srand((unsigned int)time(NULL));
	deck Deck1;
	card card1;


	Deck1.shuffle();

	return 0;
}



the headers for DECK and CARD are:
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
/*  Deck.h	Deck.h	Deck.h */
#pragma once
#include <vector>
#include "card.h"
using namespace std;


//define class 'deck'. It is a vector of cards. Functions should include shuffle, deal.
class deck
{
public:
	deck ();
	void init();
	void shuffle ();
	void deal ();
	
private:
	vector<card> myDeck;

};


/*  card.h card.h card.h */
#pragma once
using namespace std;

//define class 'card'. Card objects have no functions. They only contain data: (char)face, (char)suit, (int)pointVal.
class card
{
public:
	char face;
	char suit;
	int pointVal;
};



DECK.CPP code:

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

#include "Deck.h"
#include "card.h"
#include <stdlib.h>
#include <iostream>
#include <vector>
#define DECKSIZE 52

using namespace std;

deck::deck()  //construct a deck of cards
{
	//create en empty deck 
	vector <card> myDeck(DECKSIZE);

    //initialize the deck
	init();

}// close Deck constructor


void deck::init()
{
	//intialize the deck
	//assign suits to all 52 cards. 4 possible suits. 
	for (int i=0; i<DECKSIZE; i++)
	{
		if (i <= 12)
			myDeck[i].suit = 'c';
		else if (i <= 25)
			myDeck[i].suit = 'd';
		else if (i <= 38)
			myDeck[i].suit = 'h';
		else if (i <= 51)
			myDeck[i].suit = 's';
	} //close for loop

} 


void deck::shuffle()
{

}




**edited to fix a problem i found in the code** still not working though
Last edited on
@craduazzo

This problem jumped out at me when I first saw your listing

1
2
3
4
5
6
7
8
9
10
11
12
for (int i=0; i<DECKSIZE; i++)
	{
		if (i <= 12)
			myDeck[i].suit = 'c';
		else if (i <= 25)
			myDeck[i].suit = 'd';
		else if (i <= 38)
			myDeck[i].suit = 'h';
		else if (i <= 51) // i will ALWAYS be less than DESKSIZE ( or 52 ), so myDeck[i] will be assigned an 's'
// You could use the && command, as in 'if(i>12 && i<=25)' adjusting the numbers on the way down the for loop
			myDeck[i].suit = 's';
	} //close for loop 
Last edited on
@whitenite1
When one if condition is true, the rest are ignored. So his if/else statements are actually fine.

@OP
You create a brand new vector in your constructor. Your member variable, however, is still an empty vector. So, when you call init, everything is out of bounds. When you move your init implementation into your constructor, only the local variable is modified, leaving your member untouched.

1
2
3
deck::deck()
   : myDeck(DECKSIZE) //Called initialization list
{this->init();}


Or, if you are still unfamiliar with initialization lists, this will work fine as well:

1
2
3
4
deck::deck(){
   myDeck.resize(DECKSIZE);
   this->init();
}



Random Note:
In C++, try using the const qualifier, e.g. const size_t DECKSIZE(52);, instead of macros.
Topic archived. No new replies allowed.