#include <iostream> 
#include <string> 
#include <cstdlib> 
#include <ctime> 
using namespace std;

void displayCardFace(int value)// gives 10 value cards a face to make gameplay more realistic 
{ 
	

	if ((value >= 2) && (value <=10)) 
			cout << value; 
	else if (value ==  1) 
			cout << "Ace"; 
	else if (value == 11) 
			cout << "Jack"; 
	else if (value == 12)
			cout << "Quenn"; 
	else if (value == 13)
			cout << "King"; 
	else 
	{
		cout << "Error! Invalid card face." << endl; //error catch
		exit(0);
	} 
}

//=======================================================

void displayCardSuit(int suit)//assign card a suit value

{
	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; //error catch 
		exit (0);
	} 
	
}

//=========================================================

void displayCard(int value, int suit) 
{
	displayCardFace(value); 
	displayCardSuit(suit);
	 
}
//==========================================================

int getCardPoints(int value, int & aceValue) //assign card a face value
{
	if ((value >= 2) && (value <= 10))
		return (value); 
	else if (value == 1)
	{
		if (aceValue == 0) 
				aceValue = 10; 
		return 1; 
	}
	else if (value == 11) 
		return 10; 
	else if (value == 12) 
		return 10; 
	else if (value == 13)
		return 10;
	else 
	{		cout << "Error! Invalid card face." << endl; // error catch
		exit (0); 
	}
} 
//============================================================

int playerLoop(int playerTotal, int & aceValue) // execute the players gameplay

{
		string answer; 
		do 
	{
		cout << "Player's current hand point value is "; 
		cout <<playerTotal; 
		if (aceValue > 0) 
			cout << " or " << playerTotal + aceValue; 
			cout << endl; 
		cout << "Do you want another card? (\"hit \" or \"stay\"): ";
		getline (cin, answer);
		 if (answer == "hit")
		{
			int nextValue = (rand()%13) + 1;
			int nextSuit = (rand()%4) + 1; 
			cout << "Player is dealt ";
			displayCard(nextValue, nextSuit); 
			cout << endl; 
			playerTotal = playerTotal + getCardPoints(nextValue, aceValue);

		} 
	}while  ((answer == "hit") && (playerTotal <= 21));
	return playerTotal; 
}

//==================================================================

int dealerLoop(int dealerTotal, int & aceValue)// execute the dealers gameplay
{ 
	int bestHand; 
	if (dealerTotal + aceValue <22) 
		bestHand = dealerTotal + aceValue; 
	else 
		bestHand = dealerTotal; 

	while (bestHand < 17)// stops dealer from pulling cards at 17 to stay within blackjacks rules
	{
		cout <<"Dealer's current hand point value is "; 
		cout << dealerTotal; 
		if (aceValue > 0) 
			cout << " or " << dealerTotal + aceValue; 
		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, aceValue);
		if (dealerTotal + aceValue < 22) 
			bestHand = dealerTotal + aceValue; 
		else 
			bestHand = dealerTotal; 

	}
	return dealerTotal; 
}
void finalPoints(int playerTotal,int playerAceValue, int dealerTotal, int dealerAceValue) // use if value == 1 instead of the other stuff

{ 
	if ((playerTotal + playerAceValue) > (21))
 
		cout << "At the end, player has " << playerTotal << " or " << playerTotal + playerAceValue << " points" 
		<< endl;
	else 
	{
		cout << "At the end, player has " << playerTotal << " points" <<endl; 
	}
	if ((dealerTotal + playerAceValue) > (21))

		cout << "At the end, dealer has " << dealerTotal << " or " << dealerTotal + dealerAceValue << " points" 
		<< endl;
	else 
	{
		cout << "At the end, dealer has " << dealerTotal << " points" << endl;
	}

	// who won the game ifs 

	if ((playerTotal || playerTotal + playerAceValue) == (dealerTotal)) 
	cout <<"Draw"; 

	else if ((playerTotal && playerTotal + playerAceValue) > (dealerTotal))
		cout <<"Player wins!"; 

	else if ((dealerTotal) > (playerTotal && playerTotal + playerAceValue)) 
		cout <<"Dealer wins"; 

	else if ((playerTotal && playerTotal + playerAceValue) > (21))
		cout <<"Player busted"; 

	else if (dealerTotal > 21)
		cout <<"Dealer busted -- player wins!"; 
	else 
	{
		cout <<"Error! invalid game"; 
	}
} 

int main()

{
	//reset 
	srand((unsigned)time(0));


	//draw card
	int playerC1_value = (rand()%13) +1;
	int playerC1_suit = (rand()%4) + 1; 
	int dealerC1_value = (rand()%13) +1;
	int dealerC1_suit = (rand()%4) + 1;
	int playerC2_value = (rand()%13) +1;
	int playerC2_suit = (rand()%4) + 1; 
	int dealerC2_value = (rand()%13) +1;
	int dealerC2_suit = (rand()%4) + 1;

	/*===================================FIXED DATA
	playerC1_value = 1; 
	playerC1_suit = 1; 
	dealerC1_value = 1; 
	dealerC1_suit = 2; 
	playerC2_value = 1; 
	playerC2_suit = 3;
	//++++++++++++++++++++++++++++++++++ fixed data */

	cout << "Player has "; 
	displayCard(playerC1_value, playerC1_suit); 
	cout << " and "; 
	displayCard(playerC2_value, playerC2_suit); 
	cout << endl; 
	cout << "Dealer has "; 
	displayCard(dealerC1_value, dealerC1_suit);
	cout << " showing " << endl; 

	//figures total hand values for player and dealer below 5
	int playerAceValue = 0; 
	int dealerAceValue = 0; 

	int playerTotal = 
				  getCardPoints(playerC1_value, playerAceValue) +
				  getCardPoints(playerC2_value, playerAceValue); 
	
	

	int dealerTotal = 
				   getCardPoints(dealerC1_value, dealerAceValue) + 
				   getCardPoints(dealerC2_value, dealerAceValue); 

	playerTotal = playerLoop(playerTotal, playerAceValue);
	dealerTotal = dealerLoop(dealerTotal, dealerAceValue); 

	finalPoints(playerTotal, playerAceValue, dealerTotal, dealerAceValue);
	
}

Hi everyone, I am struggling like crazy with this program. I got the majority of everything I want working except for my finalPoints() function. I left the other code open to see if any other function is messing up that one. Any direction would be greatly appreciated.

Please explain what is going wrong. If possible, try to shorten the code and have it still produce the problem.

The problem is my else ifs wont execute in my finalPoints() function. I need the program to display who won, if it was a draw etc. The finalPoints() seems to execute but I am not getting the output that is output.

You should output the values used in the conditionals. This will show you why the "else"s aren't being hit.

std::cout << playerTotal + playerAceValue << std::endl;
if ((playerTotal + playerAceValue) > (21))

My guess is that you see something > 21.

Also, instead of generating random values, you should should hard code some values for testing. Create a case where the deal wins, another where the player wins, etc. and make sure the program operates correctly.

commented: fast and helpfull response! +1
void finalPoints(int playerTotal,int playerAceValue, int dealerTotal, int dealerAceValue) // use if value == 1 instead of the other stuff

{ 
	
	if ((playerTotal + playerAceValue) > (21))
	{
		cout << "At the end, player has " << playerTotal << " or " << playerTotal + playerAceValue << " points" 
		<< endl;
	}
	else if (playerTotal > playerTotal + playerAceValue)
	{
		cout << "At the end, player has " << playerTotal << " points" <<endl; 
	}
	else 
	{
		cout << "At the end, player has " << playerTotal + playerAceValue << " points" <<endl; 
		 
	}

	if ((dealerTotal + dealerAceValue) > (21))
	{
		cout << "At the end, dealer has " << dealerTotal << " or " << dealerTotal + dealerAceValue << " points" 
		<< endl;
	}
	else if (dealerTotal > dealerTotal + dealerAceValue) 
	{
		cout << "At the end, dealer has " << dealerTotal << " points" << endl;
	}
	else
	{
		cout << "At the end, dealer has " << dealerTotal + dealerAceValue << " points" << endl;
	}
	if ((playerTotal) && (playerTotal + playerAceValue) > (21))// 1st card needs work
	{
		cout << "Player busted" << endl; 
	}
			
	else if ((playerTotal) && (playerTotal + playerAceValue) > (dealerTotal))
	{
		cout <<"Player wins!" << endl;  
	}
	else if (dealerTotal > 21)
	{	
		cout <<"Dealer busted -- player wins!" << endl; 
	}
	else if ((playerTotal == dealerTotal)||((playerTotal + playerAceValue) == dealerTotal))
	{
	cout << "Draw" << endl;
	}
	else if ((dealerTotal) > (playerTotal && playerTotal + playerAceValue)) 
	{
		cout <<"Dealer wins" << endl; 
	}
		
	else 
	{
		cout <<"Error! invalid game" << endl; 
	}
	
}

So I made some changes and got everything operational but the logic is wacked out. It seems like when I get bust to work draw doeskin. Not just these logical evaluations but the whole series that states the outcome of the game. I have never had so much trouble with a program before. Do you guys have any ideas of how I should handle this?

hey- i'm assuming based on your program that youre in my cmpsc101 class. if so, have you figured out assignment 13? i can't for the life of me. i've spent days on it and i haven't gotten anywhere.

I'm in that class too, and 13 is killing me as well!

So I made some changes and got everything operational but the logic is wacked out. It seems like when I get bust to work draw doeskin. Not just these logical evaluations but the whole series that states the outcome of the game. I have never had so much trouble with a program before. Do you guys have any ideas of how I should handle this?

Sure. Explain in detail what the problem is. Input, output, what it should do, where it goes wrong. Details. Most of us don't have time to read and understand your code top to bottom searching for an unknown "doesn't work" problem.

Can you give the Input which is breaking the code ? Also put some log statements to print the number of points that the dealer and the player has

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.