I'm having an issue with my program when I call the "drawCardFace" and "drawCardSuit" functions. The function calculates and gives one answer the first time I call it. And then uses that same answer the next 4 times I call it. Can someone explain what I need to do to fix that? I've tried just about everything I can come up with. Thank you!

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

void drawCardFace (int & face)
{
	srand ((unsigned)time(0));
	face = (rand()%13)+1;
}
void drawCardSuit (int & suit)
{
	suit = (rand()%4)+1;
}
void displayCardFace (int face)
{
	if ((face >= 2) && (face <= 10))
		cout << face;
	else if (face == 1)
		cout << "Ace";
	else if (face == 11)
		cout << "Jack";
	else if (face == 12)
		cout << "Queen";
	else if (face == 13)
		cout << "King";
	else
	{
		cout << "Error! Invalid card face." << endl;
		exit (0);
	}
}
void displayCardSuit (int suit)
{
	if (suit == 1)
		cout << " of spades";
	else if (suit == 2)
		cout << " of clubs";
	else if (suit == 3)
		cout << " of diamonds";
	else if (suit == 4)
		cout << " of hearts";
	else
	{
		cout << "Error! Invalid card suit." << endl;
		exit (0);
	}
}
void displayCard (int face, int suit)
{
	displayCardFace (face);
	displayCardSuit (suit);
}
int getCardPoints (int face, int & aceFace)
{
	if ((face >= 2) && (face <= 10))
		return (face);
	else if (face == 1)
	{
		aceFace = 10;
		return 1;
	}
	else if (face == 11)
		return 10;
	else if (face == 12)
		return 10;
	else if (face == 13)
		return 10;
	else
	{
		cout << "Error! Invalid card face." << endl;
		exit (0);
	}
}
int playerLoop(int playerTotal, int & aceFace)
{
	string answer;
	do
	{
		cout << "Player's current hand point value is ";
		cout << playerTotal;
		if (aceFace > 0)
			cout << " or " << playerTotal + aceFace;
		cout << endl;
		cout << "Do you want another card? (\"hit\" or \"stay\"):";
		getline(cin, answer);
		if (answer == "hit")
		{
			int nextFace = (rand()%13)+1;
			int nextSuit = (rand()%4)+1;
			cout << "Player is dealt ";
			displayCard(nextFace, nextSuit);
			cout << endl;
			playerTotal = playerTotal + getCardPoints(nextFace, aceFace);
		}
	} while ((answer == "hit") && (playerTotal <= 21));
	return playerTotal;
}
int dealerLoop(int dealerTotal, int & aceFace)
{
	int bestHand;
	if (dealerTotal + aceFace < 22)
		bestHand = dealerTotal + aceFace;
	else
		bestHand = dealerTotal;
	while (bestHand < 17)
	{
		cout << "Dealer's current hand point value is ";
		cout << dealerTotal;
		if (aceFace > 0)
			cout << " or " << dealerTotal + aceFace;
		cout << endl;
		int nextValue = (rand()%13)+1;
		int nextSuit = (rand()%4)+1;
		cout << "Dealer is dealt ";
		displayCard(nextValue, nextSuit);
		cout << endl;
		dealerTotal = dealerTotal + getCardPoints(nextValue, aceFace);
		if (dealerTotal + aceFace < 22)
			bestHand = dealerTotal + aceFace;
		else
			bestHand = dealerTotal;
	}
	return dealerTotal;
}


int main()
{
	//draw a card
	int playerC1_face = 1, playerC1_suit = 1;
	int dealerC1_face = 1, dealerC1_suit = 1;
	int playerC2_face = 1, playerC2_suit = 1;
	int dealerC2_face = 1, dealerC2_suit = 1;
		
	drawCardFace(playerC1_face);
	drawCardSuit(playerC1_suit);
	drawCardFace(dealerC1_face);
	drawCardSuit(dealerC1_suit);
	drawCardFace(playerC2_face);
	drawCardSuit(playerC2_suit);
	drawCardFace(dealerC2_face);
	drawCardSuit(dealerC2_suit);


	cout << "Player has ";
	displayCard(playerC1_face, playerC1_suit);
	cout << " and ";
	displayCard(playerC2_face, playerC2_suit);
	cout << endl;
	cout << "Dealer has ";
	displayCard(dealerC1_face, dealerC1_suit);
	cout << " showing " << endl;

	int playerAceFace = 0;
	int dealerAceFace = 0;

	int playerTotal = getCardPoints(playerC1_face, playerAceFace) + getCardPoints(playerC2_face, playerAceFace);
	int dealerTotal = getCardPoints(dealerC1_face, dealerAceFace) + getCardPoints(dealerC2_face, dealerAceFace);

	cout << "So far, player has " << playerTotal << " or " << playerAceFace+playerTotal << " points" << endl;
	cout << "So far, dealer has " << dealerTotal << " or " << dealerAceFace+dealerTotal << " points" << endl;

	playerTotal = playerLoop(playerTotal, playerAceFace);
	dealerTotal = dealerLoop(dealerTotal, dealerAceFace);

	int pBestHand;
	if (playerTotal + playerAceFace < 22)
		pBestHand = playerTotal + playerAceFace;
	else
		pBestHand = playerTotal;

	cout << "At the end, player has " << pBestHand << " points" << endl;

	int bestHand;
	if (dealerTotal + dealerAceFace < 22)
		bestHand = dealerTotal + dealerAceFace;
	else
		bestHand = dealerTotal;

	cout << "At the end, dealer has " << bestHand << " points" << endl;

	if ((pBestHand == bestHand) && (pBestHand < 22) && (bestHand < 22))
		cout << "Draw" << endl;
	else if ((pBestHand > bestHand) && (pBestHand < 22) && (bestHand < 22))
		cout << "Player wins!" << endl;
	else if ((pBestHand < bestHand) && (pBestHand < 22) && (bestHand < 22))
		cout << "Dealer wins" << endl;
	else if (pBestHand >= 22)
		cout << "Player busted" << endl;
	else if (bestHand >= 22)
		cout << "Dealer busted -- player wins!" << endl;
}

Try calling srand() right at the top of main() and only do it once (delete all the other times you call it). I did this and it seemed to fix the problem with the player getting dealt the same card twice.

you're a lifesaver. thank you so much!!!

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.