Hi Guys, I'm stressing out over this project. The majority is done. I keep getting an buffer errors. I'm stuck and can't figure it out. I did call stack and it took me to the last parenthesis in main. The closing parenthesis. Also I can't figure out why my card values are not correct. I have to set my random number generator in main so that is it 1234 so my teacher can grade it. When I do that I notice that my card values are not the same the output. The first one is supposed to be 20 it's 23 I think that the misvalues are throwing off the rest of my program. I have been at it for a while please any help is appreciated.
This is my entire code

#include "functions2.h"




int main()
{
    int hitL[PLAYERCOUNT]= {0}, cardsT[PLAYERCOUNT] = {0}, 
    index = 0, i = 0;
    string playersname[PLAYERCOUNT];
    char limit = ' ' ;
    ifstream fin("input.txt");

    srand((unsigned)(1234));

    //Shuffle cards
    shuffleCards(index);

    //Input
    fin >> playersname[i] >> hitL[i];
    //Input
    while(!fin.eof())
    {   
        i++;
        fin >> playersname[i] >> hitL[i];
    }

    //Game function
    game(playersname,hitL,i,cardsT);

    system("pause");


return 0;

}

//Class
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <iomanip>

using namespace std;

class cards
{
  private:
    int face;
    char suit;

  public:
      cards(int = 0, char = ' ');
      void display();
      void swap(cards&);
      void setCards(int, char);

      //Getters
      int getFace();
      char getSuit();

};


//Functions
cards::cards(int f, char s)
{
    face = f;
    suit = s;
}

//Swap
void cards::swap(cards& c)
{
    cards temp;

    temp.face = c.face;
    temp.suit = c.suit;
    c.face = face;   
    c.suit = suit;  
    face = temp.face;
    suit = temp.suit;
}

//Display
void cards::display()
{
    switch(face)
    {
    case 11: cout <<'J';
        break;
    case 12: cout << 'Q';
        break;
    case 13: cout << 'K';
        break;
    case 1: cout << 'A' ;
        break;
    default: cout << face;
    }

    cout << suit;
}

void cards::setCards(int f, char s)
{
    face = f;
    suit = s;
}

int cards::getFace()
{   
    return face;

}

char cards::getSuit()
{
    return suit;
}
//Functions
#include "cards.h"

//Card array
cards hand[52];

//Constants

const int PLAYERCOUNT = 7;
const int GAMELIMIT = 21;
const int DEALER = 16; 
const int TWOCARD = 2;


//Functions
void Dealer(string dName[], int dHit[], int i)
{    
     i = PLAYERCOUNT;
    dName[i+1] = "Dealer";
    dHit[i+1] = DEALER;

}

void display()
{
    cout << "BlackJack by Jordan Lillie" << endl;
    cout << "The shuffled deck is : " << endl;

    for(int i = 0; i < 52; i++)
    {
        if(i % 13 == 0)
        {
            cout << endl;
        }

        hand[i].display();
        cout <<' ' << ' '; 

    }
    cout << '\n' << endl;
}

void displayHit(string playersName[], int hitLim[])
{
    cout << "Player Name" << '\t' << "Hit Limit" << endl;


    for(int i = 0; i < PLAYERCOUNT-1; i++)
    {
        setfill(" ");
        cout << right << setw(11) << playersName[i] << " " 
        << right << setw(9) << hitLim[i] << endl;
    }
        cout << endl;

}

void deal(string playersName[],int playerStat[])
{
    int counter = 0;
    cout << "Deal two cards to everyone" << endl;

    for(int i = 0; i < TWOCARD; i++)
    {
        for( int j = 0; j < PLAYERCOUNT; j++)
        {   
            playerStat[j] += hand[counter++].getFace();
        }

    }

    //Display
    for( int k = 0; k < PLAYERCOUNT; k++)
    {
        cout << "Hand for " << playersName[k] << " is : " ;
        hand[k].display();  
        cout << " and " ;
        hand[k + PLAYERCOUNT].display();
        cout << " Value for this hand is " << playerStat[k] << endl;
    }
     cout << endl << endl;
}

void limits(string playersName[], int playersStat[], int hitLim[])
{

    int hitCount = PLAYERCOUNT * TWOCARD;
    int k = 0;

    for( k = 0; k < PLAYERCOUNT; k++)
    {
        while(playersStat[k] < hitLim[k])
        {
        playersStat[k] += hand[hitCount].getFace();
        cout << "Player " << playersName[k] << " getting another card ";
        hand[hitCount].display();
        cout << endl;
        hitCount++;
        }

        cout << "Value of hand for " << playersName[k] << " is now  " << playersStat[k] << endl;
    }

}

void displayEndgame(int playerStat[], string playersName[])
{       
        cout << endl;
        cout << "Results of this game" << endl;

    for(int i = 0; i < PLAYERCOUNT - 1; i++)
    {
          if(playerStat[PLAYERCOUNT-1] == GAMELIMIT)
          {
             for( int k = 0; k < PLAYERCOUNT - 1; k++)
             {
             cout << "The deal beats player" << playersName[k] << endl;
             }
             break;
         }

        if(playerStat[i] > GAMELIMIT)
        {
            cout << "Dealer beats player " << playersName[i] << endl;
        }
    else 
    {
    if(playerStat[PLAYERCOUNT-1] <= GAMELIMIT)
    {
            if(playerStat[PLAYERCOUNT - 1] >= playerStat[i])
            {
                cout << "Dealer beats player" << playersName[i] << endl;
            }
            else 
            {
                cout << "Player " << playersName[i] << " beats the dealer" << endl;
            }
     }
           else
           {
            cout << "Player " << playersName[i] << " beats the dealer" << endl;
           }
    }
 }
          cout << endl;
}

void game(string playersName[],int hitLim[],int i, 
int playerStat[])
{
    Dealer(playersName,hitLim,i);
    display();
    displayHit(playersName, hitLim);
    deal(playersName, playerStat);
    limits(playersName, playerStat, hitLim);
    displayEndgame(playerStat, playersName);
}

void shuffleCards(int counter)
{
    for(int s = 3; s <= 6; s++)
    {
        for (int f = 1; f <= 13; f++)
        {
            hand[counter] = cards(f, char (s));
            counter++;
        }

    }

    for(int i = 0; i < 52; i++)
    {
        hand[i].swap(hand[(rand() % 52)]);

    }
    cout << endl;
}

//Input
Andy   15
Bob    17
Cathy  16
David  17
Edward 16
Freda  14

Recommended Answers

All 4 Replies

The problem with the hand value is that when you shuffle the deck, you initialize the face cards to values 11-13, which you need to distinguish what they are. But when you use them to add up a score, you must use value 10.

So you need to disginguish between the cards face for display purposes and it's scoring value. Perhaps add a getFaceValue( ) method?

Further, in your dealer function, you are adding the dealer in at index PLAYERCOUNT+1. The arrays are just PLAYERCOUNT big, go the last valid element is ....PLAYERCOUNT - 1

That error ends up corrpting the playerstat array.

Hi thanks a lot. I fixed the array problem.

>

The problem with the hand value is that when you shuffle the deck, you initialize the face cards to values 11-13, which you need to distinguish what they are. But when you use them to add up a score, you must use value 10.

So you need to disginguish between the cards face for display purposes and it's scoring value. Perhaps add a getFaceValue( ) method?

Can you please explain more? Thanks.

You are assigning the cards values 1 (ace) through 13 (King). You need those distinct values in order to display the correct card. But in the game of Blackjack, the face cards are all scored as 10 points.

So you need a function for displaying what a card is and you need another one for returning the scoring value for the cards. This second function would simply return the value of any card 10 or less, but return exactly 10 for the cards greater than 10.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.