Methods returning "incorrect" values

So I've just started getting into the C++ world and decided to make a quick Blackjack game, but it doesn't seem to work quite right.

I have a CardDeck class with a deal() method that should return the next card in the deck. It returns a PlayCard, which is defined prior. When I use my rcvcard() method within the hand property of the CardPlayer class, the deal() method is returning the same value for each CardPlayer.

May seem a bit confusing, but why is the deal() method returning "incorrect" values? I am sure the compiler is doing this properly, I'm sure I am just missing something silly. Any and all help will be appreciated.

BTW, I am using VC++ 2010. Here is the code below:

Blackjack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class CardHand;

class PlayCard {
private:
    int index; //card index 1-52, 0 is NULL
    int val; //card value 2-14, 0 is NULL
    int suit; //card suit 1-4, 0 is NULL
public:
    void assign(int); //this doesn't seem to work when called by 
	void reset() {index=0;val=0;suit=0;};
    int getindex() {return index; };
    int getval() {return val; };
    char getsuit() {return (suit + 2); };  //outputs suit symbol
    char getface();
	friend class CardHand;
};

void PlayCard::assign(int a) {
  	//assign index
  	index = a;
	//card values, 2-10, J=11, Q=12, K=13, A=14
	val = 2 + (a-1) / 4;
	//suit values, 1=Hearts, 2=Diamonds, 3=Clubs, 4=Spades
	suit = (a-1) % 4 + 1;
}

char PlayCard::getface() {
  if (val < 10) return (val + 48); //outputs char of number
  else if (val == 10) return 'T'; //output char of face from 10(T) to A
  else if (val == 11) return 'J';
  else if (val == 12) return 'Q';
  else if (val == 13) return 'K';
  else return 'A';
}

class CardHand {
private:
	PlayCard cards[5]; //5 card hand max
public:
	void rcvcard(PlayCard);
	PlayCard getcard(int n) {return cards[n]; };
	void discardall() {for(int n=0;n<=4;n++) cards[n].reset(); }; //clear card values
	bool blackjack() {if((count()==2)&&(sum()==21)) return true; else return false; };
	bool bust() {if(sum()>21) return true; else return false; };
	int sum();
	int count();
};

void CardHand::rcvcard(PlayCard a) {
	int n=count(); //get next card position
	cards[n] = a; //assign index to next card
	if((cards[n].val>10)&&(cards[n].val<14))
		cards[n].val=10; //rewrite card value for face cards
	else if(cards[n].val==14)
		cards[n].val=11; //rewrite card value for ace
}

int CardHand::sum() {
	int a=0;
	for(int n=0;n<count();n++)
		a+=cards[n].val;
	if(a>21) { //if hand total is greater than 21...
		for(int n=0;n<=count()-1;n++) {
			if(cards[n].val==11) { //search for Aces with a value of 11
				cards[n].val=1; //assign Ace value as 1
				return sum(); //get new sum
			}
		}
	}
	return a;
}

int CardHand::count() { //count number of cards in hand
	int a=0;
	for(int n=0;n<5;n++)
		if(cards[n].val>0) a++;
	return a;
}

class CardPlayer {
private:
	long score; //player money
	long bet; //current bet
public:
	CardPlayer() {score=100; };
	CardHand hand;
	long showscore() {return score; }; //display the score
	void placebet(long amt) {bet=amt; score-=bet; }; //remove bet from money
	void win() {score+=bet*2; }; //place bet and winnings in money
};

class CardDeck {
private:
	PlayCard cards[52],*top,*bottom;
	int position; //current deck position
public:
	CardDeck();
	int retpos() {return position; };
	PlayCard deal() {return cards[position++]; }; //return index at position, then increment position
};

CardDeck::CardDeck() {
	for(int n=0;n<52;n++)
		cards[n].assign(n+1);
	top=&cards[0];
	bottom=&cards[52];
	position=0; //when dealt it will return 0 then increment from there with ++
}


Blackjack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <algorithm>
#include "Blackjack.h"
#include <iostream>

using namespace std;

CardPlayer player,dealer;
CardDeck deck;

void Shuffle(), DrawScreen(bool), DealHands();

int main() {
	char ans;
	system("TITLE Blackjack++");
	cout << "Welcome to the Blackjack table!\n\n";
	cout << "Would you like to play a round? (Y/N)";
	cin >> ans;
	if((ans!='y')&&(ans!='Y')) return 0;
	Shuffle();
	DealHands();
	DrawScreen(false);
	cout << "\n\n";
	system("PAUSE");
	return 0;
}

void Shuffle() {
	player.hand.discardall();
	dealer.hand.discardall();
	//random_shuffle(deck.top,deck.bottom);
};

void DrawScreen(bool ShowDealerHand) {
	system("CLS");
	cout << "Score:  $" << player.showscore() << endl;
	cout << "Dealer: ";
	if(ShowDealerHand==true) {
		for(int n=0;n<dealer.hand.count();n++)
			cout << dealer.hand.getcard(n).getface() << dealer.hand.getcard(n).getsuit();
	}
	else {
		cout << "[]";
		for(int n=1;n<dealer.hand.count();n++)
			cout << dealer.hand.getcard(n).getface() << dealer.hand.getcard(n).getsuit();
	}
	cout << "\nPlayer: ";
	for(int n=0;n<player.hand.count();n++)
		cout << player.hand.getcard(n).getface() << dealer.hand.getcard(n).getsuit();
};

void DealHands() {
	player.hand.rcvcard(deck.deal());
	dealer.hand.rcvcard(deck.deal());
	player.hand.rcvcard(deck.deal());
	dealer.hand.rcvcard(deck.deal());
};
deck.deal() works fine when I tested it.

What makes you think it's returning the wrong value?
Oops, sorry. The function DealHands() returns the same values to player and dealer.
That's works fine when I test it too. (EDIT: to clarify, I checked both the dealer's and player's hand in the debugger after DealHands() and they had unique cards)

Why not tell me what symptoms you're seeing? What are you expecting to be output on the screen vs. what is actually being output. That kind of thing.
Last edited on
Thank you for being patient with me; so, the issue is that if you compile and run the program as shown it outputs:

Score: $100
Dealer: []2♠
Player: 2♦2♠


And if you check the values, the Dealer's first card and the Player's first card are both 2♦ and their 2nd cards are 2♠.

Why would this be happening?
1
2
3
	cout << "\nPlayer: ";
	for(int n=0;n<player.hand.count();n++)
		cout << player.hand.getcard(n).getface() << dealer.hand.getcard(n).getsuit();


Look at the last line very carefully.
ah okay.

Those suits weren't printing on mine so I had to tweak it to see what you mean.

Anyway this is a copypasta error.

Take a close look at line 48 in blackjack.cpp

EDIT: doh, too slow XD

EDIT 2:

On a side note... this is why it's generally better to give an objective description of the problem ("I was expecting X output but got Y instead"), instead of a subjective opinion about what the problem is ("My function is returning the wrong value"). Since it turns out your opinion was wrong, you had me looking for problems in code that had none.

Not that ideas about what the problem is is bad. They can help -- but don't leave out the facts.

I'm not mad or anything. I'm just saying for future reference ^^
Last edited on
I knew it had to be something stupid! Anyways, thanks for your help. This is the first time I've posted in any forum, and I like the feedback you gave me in regards to asking the question the right way.

Once again thanks for your help; hopefully next time it will be something a little more tricky.
Topic archived. No new replies allowed.