#include <iostream>
#include <Windows.h>
#include <time.h>
#include <iomanip>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;
/*********Wallet*****************/
void blingstack(double& wallet, double& winnings, double& payoff)
{
	if(payoff > 0)
		cout<< "You won $ " << winnings * payoff <<endl;
	wallet = wallet + (winnings * payoff);
	cout<<"\nYour current balance is now: $ " <<wallet<< endl;
	payoff = 0; winnings = 0;
}



/**********Suits*****************/
void Suits (string& suitarrayf)
{
	srand(time(NULL));
	int const prosuit = 4;
	int suitnum = rand()%4;
	string suitarray [prosuit] = {"Spades", "Clubs", "Diamonds", "Hearts"};
	suitarrayf = suitarray[suitnum];
}

/***********ACES*****************/
void Aces(int& acevalue)
{
	cout<<"Would you like to play your ace as 1 or 11? ";
	cin>>acevalue;

}
/***********Cards***************/
void Cards (int& cardvaluef, string& royalsf, int& royalcheckf)
{
	srand(time(NULL));
	int draw = rand()% 12 + 1;
	if (draw <11 && draw > 1)
	{
		royalcheckf = 0;
		cardvaluef = draw;
	}
	else if (draw == 1)
	{
		Aces(cardvaluef);
	}
	else
	{
		cardvaluef = 10;
		royalcheckf = 10;
		const int royalpro = 3;
		string royalsf2[royalpro] = {"Jack", "Queen", "King"};
		if(draw == 11)
		{
			royalsf = royalsf2[0];
		}
		else if(draw == 12)
		{
			royalsf = royalsf2[1];
		}
		else if(draw == 13)
		{
			royalsf = royalsf2[2];
		}
		else
		{
			cout<<"royal fuction failed, you broke my program \n";
		}
	}
}
/**************Blackjack game mechanic*******************/
void bjcompare(int playerh, int cpuh, int& resultf, string& pushf)
{
	resultf = 0;
	if (playerh == 21 && cpuh !=21)
		resultf = 3;
	else if (playerh > 21) // hand more than 21 BUST LOSE
		resultf = 1;
	else if (playerh > cpuh) // If not bust, if greater than cpu WIN
		resultf = 2;
	else if (cpuh > 21) //If not bust, but lower than cpu, but cpu bust WIN
		resultf = 2;
	else if (cpuh < 22) //if not bust, but lower than cpu, but cpu less than 22, LOSE
		resultf = 1;
	else
		pushf = "You and the Dealer Push!";
}



/**********************************************/



int main()
{
	int choice; //Game Choice Loop Variable.
	choice = 0;
	double entrywallet = 800;
	double bets = 0;
	double payoutmultiplier = 0;
	int correctbling = 0;
	

	do{

		if(entrywallet == 0)
		{
			cout<<"You have no more money, time to leave the Casino! GOODBYE!\n";
			break;
		}

		cout<<"\nPlease select which game you would like to play: ";
		cin>>choice; //game choose
		
		switch(choice)
		{
			case 1:
				{
					cout<<"\nWelcome to BlackJack\n";
					
					blingstack(entrywallet, bets, payoutmultiplier);
					
					cout<<"\nLets Get Started!\n\n";

					do{
						cout<<"How much would you like to bet? ";
						cin>> bets;
						if(entrywallet == 0)
						{
							cout<<"You have no more money, time to leave the Casino! GOODBYE!\n";
							break;
						}
						else if(bets > entrywallet)
						{
							cout<<"You don't have that much money!\n\n";
						}
						else if(bets < 0)
						{
							cout<<"You can't give money away!\n\n";
						}
						else if(bets == entrywallet)
						{
							cout<<"Your ALL IN! GOOD LUCK!\n\n";
							correctbling = 1;
						}
						else
							correctbling = 1;
					}while(correctbling == 0);

					cout<<"\nAlright! Here comes the deal!\n\n";

					Sleep(2000);
		
					
		/*************Front Varibles for Function Calls*************/
					int phand, chand, draw;
					const int pcardvalue = 2;
					int pcard[pcardvalue];
					int cardvalue = 0; int royalcheck = 0;
					string royals;
					string suitsdisp;

			/*******************Player Card Draw Sequence*********/
					for(int pcardvalue=0; pcardvalue<2; pcardvalue++) //loop to draw and store player draws in an array.
					{	
						Cards(cardvalue, royals, royalcheck); //run card draw function
						Suits(suitsdisp); //run random suit function
						pcard[pcardvalue] = cardvalue;
						if(cardvalue == 1 || cardvalue == 11)
						{
							cout<< "Ace of "<< suitsdisp << endl; //display value of card according assigned array, then display suit.
						}
						else if(royalcheck != 10)
						{
							cout<< pcard[pcardvalue] << " of " << suitsdisp << endl;
						}
						else if(royalcheck == 10)
						{
							cout<<royals<< " of " << suitsdisp << endl;
						}
							Sleep (2000); //Allow time for rand seed to catch up
					}
						
					phand = pcard[0] + pcard[1];
					cout<<"\nYour total is " << phand << "\n\n";
			/*****************************Hit or Stay*********************/
					string hs;
					int error = 0;
					do{
						cout<<"Would you like to Hit or Stay? ";
						cin>> hs;
						transform (hs.begin(), hs.end(), hs.begin(), tolower); //input case insensitivity

						if(hs == "hit")
						{
							Cards(cardvalue, royals, royalcheck); //run card draw function
							Suits(suitsdisp); //run random suit function
							draw = cardvalue;
							if(cardvalue == 1 || cardvalue == 11)
							{
								cout<< "Ace of "<< suitsdisp << endl; //display value of card according assigned array, then display suit.
							}
							else if(royalcheck != 10)
							{
								cout<< draw << " of " << suitsdisp << endl;
							}
							else if(royalcheck == 10)
							{
								cout<<royals<< " of " << suitsdisp << endl;
							}
						}
						else if(hs == "stay")
							break;
						else
						{
							cout<<"You've entered something invalid, try again\n";
							error = 1;
						}
		
						phand = phand + draw;
						cout<<"\nYour total is " << phand << "\n\n";
						if(phand > 21)
						{
							cout<<"You busted! Good Luck Next Time!\n";
							break;
						}
						else{}

					}while(hs != "stay" || error == 1);





			/*********************CPU Card Draw Sequence*************/
					chand = rand()% 8 + 16;
					
					cout<<"\nComputer's total is " << chand << "\n\n";
				
		/*********************END OF INITIAL CARD DRAW*********START HIT/STAY SEQUENCE**************/
					int result = 0;
					string push;
					bjcompare(phand, chand, result, push);

					if (result == 3)
					{
						cout<<"You got BlackJack! YOU WIN EXTRA!!! \n";
						payoutmultiplier = 2.5;
						blingstack(entrywallet, bets, payoutmultiplier);
					}
					else if(result == 2)
					{
						cout<<"You win! Congratulations!\n";
						payoutmultiplier = 2;
						blingstack(entrywallet, bets, payoutmultiplier);
					}
					else if(result == 1)
					{
						cout<<"You Lose! Good Luck Next Time!\n";
						payoutmultiplier = -1;
						blingstack(entrywallet, bets, payoutmultiplier);
					}
			

					else
						cout<< push << endl;


			/*********************END OF GAME***********************/

I'm having an issue with invalid inputs, such as when someone inputs a letter when asked for "How much do you want to bet" , it would go ahead an run the problem indefinitely and print out really high numbers or really negative numbers as my balance.
Along those lines similar problem occurs when asked to hit or stay, but it just doesn't run indefinitely, it just prints out those extreme numbers.
Pretty much I'm having issues controlling invalid inputs, it seems as though my if else statements are being bypassed?

Read all inputs as strings. Then test to see if all characters are digits and if so, convert to a number and continue. Otherwise, process an error.

I searched around and it seems as though reading all input as string then converting it requires programming a bit beyond my current knowledge. Is there another way of approaching this?

If it's beyond your current knowledge, put the task in the shelf for a few weeks and come back to it. You will probably have enough knowledge by then to start tackling the job.

Or search the forums. This has been answered a lot!

hey waltp, thanks for putting up with me, so I fixed one of the problems regarding having a $0 balance and the game would continue by using

bool exitswitch = false; // on the outside of the switch statement
exitswitch = true; // within the loop cased inside the switch
if(exitswitch == true) break; // in the switch

now I'm not sure if that was a good way of doing it, but it works....
as for inputing a letter instead of a number, causing the program to loop endlessly, I'm guessing were back to conversions?

nevermind, I was able to fix the endless loop by adding a

cin.clear();
cin.ignore();

in my else statement.

also fixed invalid input for HIT or Stay by adding an extra if else statement to check for an error value assigned if an invalid input was given.

hey waltp, thanks for putting up with me, so I fixed one of the problems regarding having a $0 balance and the game would continue by using

bool exitswitch = false; // on the outside of the switch statement
exitswitch = true; // within the loop cased inside the switch
if(exitswitch == true) break; // in the switch

Great!!

now I'm not sure if that was a good way of doing it, but it works....
as for inputing a letter instead of a number, causing the program to loop endlessly, I'm guessing were back to conversions?

Yep...

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.