Deck of Cards

Hey folks! I am trying to set up a class that creates a deck of cards, shuffles it and display it. my code doesn't display an output, but also doesn't show any compiler errors.

1
2
3
4
5
6
7
8
9
10
//DeckOfCards.h
class Deckofcards
{
public:
	Deckofcards();
	void shuffle();
	void deal();
private:
	int deck[4][13];
};


1
2
3
4
5
6
7
8
9
10
11
//client.cpp
#include "DeckOfCards.h"

int main()
{
	Deckofcards deckofcards;

	deckofcards.shuffle();
	deckofcards.deal();
	return 0;
}//end main 


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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Deckofcards.cpp

#include <iostream>
using std::cout;
using std::left;
using std::right;

#include <iomanip>
using std::setw;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

#include "DeckOfCards.h"

Deckofcards::Deckofcards()
{
	for (int row = 0; row <= 3; row++ )
	{
		for (int column = 0; row <= 12; column++)
		{
			deck[row][column] = 0; //slot of deck is 0
		}//end column for
	}//end row for
	
	srand(time(0));//seed random number

}//end Deckofcards constructor

void Deckofcards::shuffle()
{
	cout << "hit";
	int row; //suit of card
	int column; //face of card

	for (int card = 1; card <=52; card++)
	{
		do
		{
			row = rand()%4;
			column = rand()%13;
		} while( deck[row][column] !=0);//end do...while
		deck [row][column] = card;
	}//end for
}//end shuffle

void Deckofcards::deal()
{
	cout << "hit";
	static const char *suit[4] = {"Hearts", "Diamonds", "Clovers", "Spades"};
	static const char *face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven"
		, "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
	for (int card = 1; card <=52; card++)
	{
		for (int row = 0; row <= 3; row++)
		{
			for (int column = 0; column <= 12; column++)
			{
				if (deck[row][column] == card)
				{
				cout << setw(5) << right << face[column] << " of " << setw(8) 
					<< left << suit[row] << (card %2 == 0 ? '\n' : '\t');
				}//end if
			}//end for
		}//end for
	}//end for
}//end deal 



This is also a stepping stone in my school project, which is Blackjack. If you have any suggestions on that as well it would be greatly appreciated! Thanks!
Line 24 in Deckofcards.cpp:
for (int column = 0; row <= 12; column++){
Your condition is false and the array is going out of limits. It should be "column <= 12", not row...
I knew it would be something stupid like that! thanks! that fixed it.
jeep, this is a very strange way to store a deck of cards. Not to mention the runtime of your constructor is O(infinity). Why did you choose to store the deck in this fashion? Did you explore alternative solutions? For example, since a deck is almost always accessed from top to bottom, wouldn't a one-dimensional data structure work better than a 2D array?
O(infinity)
O(you)!

The data structure is the least strange here. Just look at deal(). It's like a map gone wrong.
Topic archived. No new replies allowed.