I am totally lost and confused on this program. I have most of it finished but I
ID: 3624532 • Letter: I
Question
I am totally lost and confused on this program. I have most of it finished but I do not know how to determine who wins, how to stop the game, and how to ask the user if they would like to play again. Also I am just using 11 cards to make it easier. Here is what I have so far. I need this pretty quick before 9 est please#include <iostream>
#include <ctime>
using namespace std;
double card = 0;
double pHand = 0;
double dHand = 0;
int YN = 0;
bool val = true;
int main()
{
srand( time( NULL ) );
card = ( rand() %11) ; // opening hand
cout << card << " has been drawn" <<endl;
pHand = pHand+card;
cout << "Player has " << pHand<<" "<< endl;
card = ( rand() %11) ;
cout << card << " has been drawn" <<endl;
dHand = dHand+card;
cout << "Dealer has " << dHand<<" "<<endl;
card = ( rand() %11);
cout << card << " has been drawn" <<endl;
pHand = pHand+card;
cout << "Player has " << pHand<<" "<< endl;
card = ( rand() %11);
cout << card << " has been drawn" <<endl;
dHand = dHand+card;
cout << "Dealer has " << dHand<<" "<< endl;
while (val == true)
{
cout << "Would you like another card?(y/n) " ;
cin >> YN;
if (YN = 'Y')
{
val == true;
}
if (YN != 'N')
{
val == false;
}
if (val == true)
{
card = ( rand() %11) ;
cout << card << " has been drawn" <<endl;
pHand = pHand+card;
cout << "Player has " << pHand<<" "<< endl;
}
if (val == false)
{
while (dHand <= 17)
{
card = ( rand() %11);
cout << card << " has been drawn" <<endl;
dHand = dHand+card;
cout << "Dealer has " << dHand<<" "<< endl;
}
}
if(pHand>dHand){
cout<<"You Win"<<endl;
}
if(pHand<dHand){
cout<<"You Lose"<<endl;
}
}
system ("pause");
return 0;
}
Explanation / Answer
please rate - thanks
sorry
don't have time to fix yours maybe this will help. you can get rid of the betting easily
#include <iostream>
#include <ctime>
#include<iomanip>
using namespace std;
void deal(int,int&);
void deal(int,int&,int&);
int getbet(int);
int main()
{
int totdealer,totplayer;
int money,bet,aces;
char yesno='Y';
bool again;
srand(time(0));
cout<<"How much money do you have? ";
cin>>money;
while(money>0&&toupper(yesno)=='Y')
{bet=getbet(money);
totdealer=0;
deal(2,totdealer);
aces=0;
totplayer=0;
deal(2,aces,totplayer);
cout<<"Your total is "<<totplayer<<endl;
cout<<"You have "<<aces<<" aces"<<endl;
again=false;
while(totdealer<21&&totplayer<21&&!again)
{
cout<<" do you want another card? ";
cin>>yesno;
if(toupper(yesno)=='Y')
{deal(1,aces,totplayer);
cout<<"Your total is "<<totplayer<<endl;
cout<<"You have "<<aces<<" aces"<<endl;
}
else
again=true;
}
cout<<"Dealer total: "<<totdealer<<endl;
cout<<"Your total: "<<totplayer<<endl;
if(totdealer>totplayer||totplayer>21)
money-=bet;
else if(totdealer<totplayer&&totplayer<=21)
money+=bet;
cout<<" Game over You have $"<<money<<" left ";
if(money>0)
{cout<<" Play again (Y/N)?";
cin>>yesno;
}
}
system("pause");
return 0;
}
void deal(int num,int& tot)
{int i,n;
for(i=0;i<num;i++)
{n=rand()%13+1;
if(n==1)
tot+=11;
else if(n>=10)
tot+=10;
else
tot+=n;
}
}
void deal(int num,int& aces,int &tot)
{int i,n;
for(i=0;i<num;i++)
{n=rand()%13+1;
if(n==1)
{aces++;
tot+=11;
}
else if(n>=10)
tot+=10;
else
tot+=n;
}
if(tot>21)
if(aces==0)
return;
else
{aces--;
tot-=10;
}
}
int getbet(int money)
{int bet;
cout<<"You have $"<<money<<" enter your bet: ";
cin>>bet;
while(bet>money)
{cout<<"You cannot bet more then you have!!! ";
cout<<"You have $"<<money<<" enter your bet: ";
cin>>bet;
}
return bet;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.