Thanks for responding. That may be a case with left over garbage in the input stream. The previous question is "Do you want to play another hand?" with cin>>yesno, where yesno is a char. I then checked yesno against 'y' and 'Y' in order to avoid problems where they input just a Y instead of Yes.
The entire code upto Main() is as follows, incase what was previously posted wouldn't of left garbage. Still doesn't answer to me how inputting bet=0 immediately before the while statement would fix garbage in the stream.
"goodbet" is a bool, it is a global variable. It is (or will be used often) whenever checking to make sure the bet/insurance etc is valid. In this scenario it makes sure the person bet's more then $10, which is the mininum as per professors instructions. In other parts it is to make sure people don't bet negatively and then lose on purpose... though in hindsight if I made it an unsigned int that could prevent people from betting negative money, however I am not too sure what an unsigned int is. Just that a signed int uses the first bit(?) to show pos/neg and an unsigned int does not have that so it can't be negative. What would happen if someone tried putting a negative number into an unsigned int?
As I previously said all or almost all global variables will become part of a CardGame class which each game will derive from. I keep them global for now until I fulfill the assignment requirements so that they are all in one place before I go ahead with what I want to do.
~edit is there a way to make the code scrollable or is that done auto at a certain length?
Code:
#include <conio.h>
#include <iostream>
#include <list>
#include <cstdlib>
#include <ctime>
#include <string.h>
using namespace std;
int money=0,pool=0,bet=0;
bool ante;
char yesno;
bool getbet,goodbet;
struct Card
{
int rank;
char suit;
int value;
}C;//end of Card
struct Hand
{
list<Card> main;
list<Card> side;
list<Card>::iterator hi;
}Dealer,Player;//end of hand
class Deck
{
public:
void TakeCard(Card);
Card GetCard();
Deck Shuffle(Deck,Deck);
Deck();
Deck(int);
private:
list<Card> D1;
list<Card>::iterator di;
}MainD,TempD(0);//End of Deck Class
Deck::Deck()
{
int suitcounter=0,rankcounter=2;
while (suitcounter!=4)
{
while (rankcounter!=15)
{
C.suit=suitcounter;
C.rank=rankcounter;
if (rankcounter>10&&rankcounter<14)
{C.value=10;}
else
{C.value=rankcounter;}
D1.push_front(C);
++rankcounter;
}
rankcounter=2;
++suitcounter;
}
}//end of Deck::Deck
Deck::Deck(int x)
{}
void Deck::TakeCard(Card C)
{
D1.push_front(C);
}//end of TakeCard
Card Deck::GetCard()
{
Card tempCard;
list<Card> temp;
list<Card>::iterator ti;
srand((unsigned)time(0));
int random,x;
x=D1.size();
random=(rand()%x);
while (random!=0)
{
di=D1.begin();
C=*di;
//if (di!=D1.end())
//{
//++di;
//}
D1.pop_front();
temp.push_front(C);
--random;
}
di=D1.begin();
tempCard=*di;
D1.pop_front();
while (temp.size()!=0)
{
ti=temp.begin();
C=*ti;
//if (ti!=temp.end())
//{
//++ti;
//}
temp.pop_front();
D1.push_front(C);
}
return(tempCard);
}// end of GetCard
Deck Deck::Shuffle(Deck temp,Deck main)
{
int x;
x=(int) temp.D1.size();
while (x!=0)
{
C=temp.GetCard();
main.TakeCard(C);
--x;
}
return main;
}//end of shuffle
class BlackJack
{
private:
int insurance;
int deals;
int sbet;
int value;
bool again;
bool duble;
bool split;
public:
BlackJack(int);
Card Hit();
void Fold();
void Split();
void Double();
void Surrender();
void NewHand();
void Insurance();
};//end of BlackJack declarations
BlackJack::BlackJack(int cash)
{
ante=true;
again=true;
money=cash;
cout<<"Hello and welcome to BlackJack. You start with $"<<money<<". Good luck."<<endl;
_getche();
while (again==true)
{
if (money<10)
{
ante=false;
}
cout<<"Play a hand?(Yes/No)"<<endl;
cin>>yesno;
if (yesno=='n'||yesno=='N')
{
again=false;
}
if (again==true&&ante==false)
{
cout<<"Sorry you don't have enough funds for the mininum bet(10)";
again=false;
}
if (again==true&&ante==true)
{
deals=deals+1;
if (deals==6)
{
MainD.Shuffle(TempD,MainD);
deals=1;
}
BlackJack::NewHand();
}
}
MainD.Shuffle(TempD,MainD);
}//end of constructor
void BlackJack::NewHand()
{
string choice;
Card Compare;
getbet=false;
goodbet=false;
bet=0;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
while (getbet==false)
{
cout<<"How much would you like to bet?"<<endl<<" You have $"<<money<<"."<<endl;
cin>>bet;
if (bet>10)
{goodbet=true;}
if (bet<money&&goodbet==true)
{
money=money-bet;
pool=pool+bet;
getbet=true;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
getbet=false;
goodbet=false;
C=MainD.GetCard();
Player.main.push_front(C);
C=MainD.GetCard();
Dealer.main.push_front(C);
C=MainD.GetCard();
Player.main.push_front(C);
C=MainD.GetCard();
Dealer.main.push_front(C);
Player.hi=Dealer.main.begin();
C=*Player.hi;
if (C.rank==12)
{
cout<<"The dealer has an ace, would you like insurance?(Yes/No)";
cin>>yesno;
if (yesno=='y'||yesno=='Y')
{
Insurance();
/*while (getbet=false)
{
cout<<"You currently have $"<<money<<", you may insure upto $"<<bet/2<<".";
cin>>insurance;
if(insurance>=0)
{goodbet=true;}
if (insurance<money&&insurance<(bet/2)&&goodbet==true)
{
money=money-insurance;
Dealer.hi=Dealer.main.end();
C=*Dealer.hi;
if (C.value==10)
{
cout<<"The dealer has BlackJack, you win the insurance";
money=money+2*(insurance);
}
insurance=0;
}
else
{cout<<"Your insurance bet did not meet the requirements, bet again";}
}*/
}
}
Player.hi=Player.main.begin();
C=*Player.hi;
Player.hi=Player.main.end();
Compare=*Player.hi;
cout<<"Your cards are a "<<C.rank<<" of "<<C.suit<<" and a "<<Compare.rank<<" of "<<Compare.suit<<".";
if (C.rank==Compare.rank)
{split=true;}
if (split==true)
{
cout<<"Would you like you Split?";
cin>>yesno;
if (yesno=='y'||yesno=='Y')
{
if (money>=bet)
{
Split();
}
else
{
cout<<"Not enough cash to split.";
}
}
}
}