Array/Pointer is causing a STATUS_ACCESS_VIOLATION is g++

S

SB

Hello! I have two classes, Player and CardDeck. CardDeck contains an int
array called drawCards(int drawCards[12];). Among other things, the Player
class contains a static CardDeck object(static CardDeck c;). These are both
in a header file. From my other .cpp file with main, I have an array of
players (Player players[11];). My goal is to give access to a single deck of
cards (drawCards) that was initialized in the CardDeck class to the Player
class - obviously, it has to be static because all 11 players need to share
the same deck of cards - not each have their own deck. I was able to
accomplish this and have verified the values in the drawCard array in the
Player class are matching the values of the drawCards deck in the CardDeck
class.
Now for the problem...everything worked fine until I'm assigning each player
from the player array a card (an int value) out of the drawCards array.
Everytime it will assign 9 players a card, then I get this message:
(actually, I'll paste the output to show that the first 9 are getting a
card)
a draws a 5 of Spades
b draws a Jack of Spades
c draws a 7 of Spades
d draws a 6 of Spades
e draws a 2 of Spades
f draws a Queen of Spades
g draws a 8 of Spades
h draws a King of Spades
i draws a 9 of Spades
[main] C:\Program Files\Microsoft Visual Studio\MyProjects\Holdem\a.exe 1000
(0)
handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
[main] a 1000 (0) handle_exceptions: Dumping stack trace to a.exe.core

Here is the code that I think anyone will need to look at to help me solve
this, and I'll add a comment on the line that I believe is causing the
error:
From the CardDeck class:
int drawCards[12];

From the Player class:
int cardsInHand[2];

// gets the draw cards
int* CardDeck::getDrawCards()
{
// show the cards
test(drawCards);

return drawCards;
}

void CardDeck::test(int a[])
{
for (int i = 0; i < 12; i++)
cout<<"test card is "<<a<<endl;
}

void Player::drawCards(Player players[], CardDeck c)
{
int drawCardRank = 0;
int drawCardSuit = 0;

// get the cards
int* cards = c.getDrawCards(); //*NOTE* getDrawCards returns a pointe, but
i return it drawCards array. is this correct? wouldn't compile otherwise
cout<<endl;
//c.test(cards); //*NOTE* if i uncomment this, all 12 cards will display
without error, so i know the array values are not the problem
for (int i = 0; i < numberOfPlayers; i++)
{
// each player draws one card
players.cardsInHand = cards; //*NOTE* I believe this is the
problematic line
// map the card
drawCardSuit = players.cardsInHand / 100; // get the actual suit for
each card
drawCardRank = players.cardsInHand % 100; // get the actual rank for
each card
*NOTE* mapCards simply returns a string for the rank and suit so the
user will see an actual card instead of an int value
cout<<players.name<<" draws a "<<c.mapCards(drawCardRank,
drawCardSuit)<<endl;
}
}

Here is main, in its entirety:
#include "Holdem.cpp"

using namespace std;

int main()
{
srand(time(0));
CardDeck cards;
// this needs to be a 11 element array - 10 players, one dealer
Player players[11];
players[0].initializePlayerArray(players); // all this does is gives access
to the array of players from main to the functions in the player class
players[0].drawCards(players, cards);
return 0;
}

Can anyone help me figure this out? My assumption is that I'm just not
assigning the first element of the cardsInHand array of each player a value
from the drawCards array properly, but I don't know how else to do it.

Thanks in advance!!!
 
R

Ron Natalie

SB said:
for (int i = 0; i < numberOfPlayers; i++)
{
// each player draws one card
players.cardsInHand = cards; //*NOTE* I believe this is the
problematic line


Of course it's the problematic line i numberOfPlayers is greater than 2.
Just what do you think you are doing using the same index everywhere.
cardsInHand is only two elements.

It would appear you want seperate indexes for all three of those arrays.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top