C++ Deck and Card Class Error with bad alloc
Posted
by
user3702164
on Stack Overflow
See other posts from Stack Overflow
or by user3702164
Published on 2014-06-03T08:11:24Z
Indexed on
2014/06/03
9:25 UTC
Read the original article
Hit count: 271
Just started learn to code in school. Our assignment requires us to create a card game with card,deck and hand class. I am having troubles with it now and i keep getting exception: std::bad_alloc at memory location. Here are my codes right now
CardType h:
#ifndef cardType_h
#define cardType_h
#include <string>
using namespace std;
class cardType{
public:
void print();
int getValue() const;
string getSymbol() const;
string getSpecial() const;
string getSuit() const;
int checkSpecial(int gscore) const;
cardType();
cardType(string suit,int value);
private:
int value;
string special;
string symbol;
string suit;
};
#endif
CardType cpp:
#include "cardType.h"
#include <iostream>
#include <string>
using namespace std;
void cardType::print()
{
cout << getSymbol() << " of " << getSuit() << ", having the value of " << getValue() << "."<< endl <<"This card's special is " << getSpecial() << endl;
}
int cardType::getValue() const
{
return value;
}
string cardType::getSymbol() const
{
return symbol;
}
string cardType::getSpecial() const
{
return special;
}
string cardType::getSuit() const
{
return suit;
}
cardType::cardType(){
value=0;
symbol="?";
special='?';
suit='?';
}
cardType::cardType(string s, int v){
suit = s;
value = v;
switch(v){
case 1: // Ace cards have a value of 1 and have no special type
symbol="Ace";
special="None";
break;
case 2: // 2 cards have a value of 2 and have no special type
symbol="2";
special="None";
break;
case 3:
symbol="3"; // 3 cards have a value of 3 and have no special type
special="None";
break;
case 4:
symbol="4"; // 4 cards have a value of 0 and have a special type "Reverse" which reverses the flow of the game
special="Reverse";
value=0;
break;
case 5:
symbol="5"; // 5 cards have a value of 5 and have no special type
special="None";
break;
case 6:
symbol="6"; // 6 cards have a value of 6 and have no special type
special="None";
break;
case 7:
symbol="7"; // 7 cards have a value of 7 and have no special type
special="None";
break;
case 8:
symbol="8"; // 8 cards have a value of 8 and have no special type
special="None";
break;
case 9:
symbol="9"; // 9 cards have a value of 0 and have a special type "Pass" which does not add any value to the game and lets the player skip his turn.
special="Pass";
value=0;
break;
case 10:
symbol="10"; // 10 cards have a value of 10 and have a special type "subtract" which instead of adding the 10 value to the total game it is subtracted instead.
special="Subtract";
value=10;
break;
case 11: // Jack cards have a value of 10 and have no special type
symbol="Jack";
special="None";
value=10;
break;
case 12: // Queens cards have a value of 10 and have no special type
symbol="Queen";
special="None";
value=10;
break;
case 13:
symbol="King"; // King cards have a value of 0 and have a special type "NinetyNine" which changes the total game score to 99 reguardless what number it was previously
special="NinetyNine";
value=0;
break;
}
}
int cardType::checkSpecial(int gscore) const{
if(special=="Pass"){
return gscore;
}
if(special=="Reverse"){
return gscore;
}
if(special=="Subtract"){
return gscore - value;
}
if(special=="NinetyNine"){
return 99;
}
else{
return gscore + value;
}
}
DeckType h:
#ifndef deckType_h
#define deckType_h
#include "cardType.h"
#include <string>
using namespace std;
class deckType
{
public:
void shuffle();
cardType dealCard();
deckType();
private:
cardType *deck;
int current;
};
#endif
DeckType cpp:
#include <iostream>
#include "deckType.h"
using namespace std;
deckType::deckType()
{ int index = 0;
int current=0;
deck = new cardType[52];
string suit[] = {"Hearts","Diamonds","Clubs","Spades"};
int value[] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
for ( int i = 0; i <= 3; i++ ) {
for ( int j = 1; j <= 13; j++ ) {
deck[index] = cardType(suit[i],value[j]);
index++;
}
}
}
cardType deckType::dealCard()
{
return deck[current];
current++;
}
Main cpp :
#include "deckType.h"
#include <iostream>
using namespace std;
int main()
{
deckType gamedeck;
cout << "1" <<endl;
cardType currentCard;
cout << "2" <<endl;
currentCard = gamedeck.dealCard();
cout << "3" <<endl;
return 0;
}
I keep getting bad_alloc at the currentCard = gamedeck.dealCard(); I really do not know what i have done wrong.
© Stack Overflow or respective owner