Hyena
May 5th, 1999, 10:47 AM
i am making a blackjack simulation program; the following code constructs and prints the deck. however, it doesn't print the value of nonface cards, printing only a blank. i can't figure out whats wrong.
CODE:
//program Cards1: blackjack
#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#include <stddef.h>
//---------------------------struct Name-----------------------------
struct Name {
char* suit;
char* type;
};
//-------------------------------------------------------------------
//-------------------------struct oneCard----------------------------
struct oneCard {
int value;
Name name;
};
//-------------------------------------------------------------------
//------------------------class backjack-----------------------------
class blackjack {
public:
initDeck();
//creates a deck of cards
oneCard card[52];
};
//--------------------------------------------------------------------
//-------------------------member functions---------------------------
/********************************************************************/
blackjack::initDeck()
//post: deck is created
{
int cardVal = 2;
int suitNum = 1;
for(int x = 0; x < 52; x++)
{
card[x].name.suit = new char[10];
card[x].name.type = new char[10];
card[x].name.type = " ";
}
for(x = 0; x < 36; x++)
{
card[x].value = cardVal;
switch(suitNum)
{
case 1 :card[x].name.suit = "hearts";
break;
case 2 :card[x].name.suit = "spades";
break;
case 3 :card[x].name.suit = "clubs";
break;
case 4 :card[x].name.suit = "diamonds";
break;
}
if(suitNum < 4) { suitNum++; }
else {
suitNum = 1;
cardVal++;
}
}
int cardType = 1;
suitNum = 1;
for(x = 36; x < 48; x++)
{
card[x].value = cardVal;
switch(suitNum)
{
case 1 :card[x].name.suit = "hearts";
break;
case 2 :card[x].name.suit = "spades";
break;
case 3 :card[x].name.suit = "clubs";
break;
case 4 :card[x].name.suit = "diamonds";
break;
}
if(suitNum < 4) { suitNum++; }
else {
suitNum = 1;
}
switch(cardType)
{
case 1:
case 2:
case 3:
case 4 :card[x].name.type = "jack";
break;
case 5:
case 6:
case 7:
case 8 :card[x].name.type = "queen";
break;
case 9:
case 10:
case 11:
case 12 :card[x].name.type = "king";
break;
}
cardType++;
}
suitNum = 1;
for(x = 48; x < 52; x++)
{
card[x].name.type = "ace";
card[x].value = NULL;
switch(suitNum)
{
case 1 :card[x].name.suit = "hearts";
break;
case 2 :card[x].name.suit = "spades";
break;
case 3 :card[x].name.suit = "clubs";
break;
case 4 :card[x].name.suit = "diamonds";
break;
}
if(suitNum < 4) { suitNum++; }
else {
suitNum = 1;
}
}
}
/*******************************************************************/
//____________________________-MAIN-_________________________________
void main()
{
blackjack b;
b.initDeck();
for(int y = 0; y < 52; y++)
{
if((b.card[y].value != NULL) && (b.card[y].name.type == " ")) //prints regular cards
{
cout << b.card[y].value;
cout << " of " << b.card[y].name.suit << "\n";
}
else if((b.card[y].value != NULL) && (b.card[y].name.type != " ")) //prints face cards
{
cout << b.card[y].name.type;
cout << " of " << b.card[y].name.suit << "\n";
}
else
{
cout << b.card[y].name.type << " of " << b.card[y].name.suit << "\n"; //prints aces
}
}
CODE:
//program Cards1: blackjack
#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#include <stddef.h>
//---------------------------struct Name-----------------------------
struct Name {
char* suit;
char* type;
};
//-------------------------------------------------------------------
//-------------------------struct oneCard----------------------------
struct oneCard {
int value;
Name name;
};
//-------------------------------------------------------------------
//------------------------class backjack-----------------------------
class blackjack {
public:
initDeck();
//creates a deck of cards
oneCard card[52];
};
//--------------------------------------------------------------------
//-------------------------member functions---------------------------
/********************************************************************/
blackjack::initDeck()
//post: deck is created
{
int cardVal = 2;
int suitNum = 1;
for(int x = 0; x < 52; x++)
{
card[x].name.suit = new char[10];
card[x].name.type = new char[10];
card[x].name.type = " ";
}
for(x = 0; x < 36; x++)
{
card[x].value = cardVal;
switch(suitNum)
{
case 1 :card[x].name.suit = "hearts";
break;
case 2 :card[x].name.suit = "spades";
break;
case 3 :card[x].name.suit = "clubs";
break;
case 4 :card[x].name.suit = "diamonds";
break;
}
if(suitNum < 4) { suitNum++; }
else {
suitNum = 1;
cardVal++;
}
}
int cardType = 1;
suitNum = 1;
for(x = 36; x < 48; x++)
{
card[x].value = cardVal;
switch(suitNum)
{
case 1 :card[x].name.suit = "hearts";
break;
case 2 :card[x].name.suit = "spades";
break;
case 3 :card[x].name.suit = "clubs";
break;
case 4 :card[x].name.suit = "diamonds";
break;
}
if(suitNum < 4) { suitNum++; }
else {
suitNum = 1;
}
switch(cardType)
{
case 1:
case 2:
case 3:
case 4 :card[x].name.type = "jack";
break;
case 5:
case 6:
case 7:
case 8 :card[x].name.type = "queen";
break;
case 9:
case 10:
case 11:
case 12 :card[x].name.type = "king";
break;
}
cardType++;
}
suitNum = 1;
for(x = 48; x < 52; x++)
{
card[x].name.type = "ace";
card[x].value = NULL;
switch(suitNum)
{
case 1 :card[x].name.suit = "hearts";
break;
case 2 :card[x].name.suit = "spades";
break;
case 3 :card[x].name.suit = "clubs";
break;
case 4 :card[x].name.suit = "diamonds";
break;
}
if(suitNum < 4) { suitNum++; }
else {
suitNum = 1;
}
}
}
/*******************************************************************/
//____________________________-MAIN-_________________________________
void main()
{
blackjack b;
b.initDeck();
for(int y = 0; y < 52; y++)
{
if((b.card[y].value != NULL) && (b.card[y].name.type == " ")) //prints regular cards
{
cout << b.card[y].value;
cout << " of " << b.card[y].name.suit << "\n";
}
else if((b.card[y].value != NULL) && (b.card[y].name.type != " ")) //prints face cards
{
cout << b.card[y].name.type;
cout << " of " << b.card[y].name.suit << "\n";
}
else
{
cout << b.card[y].name.type << " of " << b.card[y].name.suit << "\n"; //prints aces
}
}