CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 1999
    Location
    wallingford, pennsylvania
    Posts
    70

    blackjack program

    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
    }


    }



  2. #2
    Join Date
    May 1999
    Posts
    28

    Re: blackjack program

    Not sure, but it looks like you forgot:

    cout << b.card[y].name.type << " of "

    for the numbered cards


  3. #3
    Join Date
    May 1999
    Location
    wallingford, pennsylvania
    Posts
    70

    Re: blackjack program

    no no. card[y].name.type is a variable storing a face value- king, queen, jack, or ace. card[].value is what stores value, and card[].name.suit stores suit- hearts, clubs, spades, and diamonds.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured