Hello to all! This is my first post and I am hoping for some help....
I am trying to get my first mid term done in my first ever C++ course, and I am stuck!
I am puting together a simple BlackJack game with beginner code, and having trouble passing a value from one function to another. Here is the code (commented) where i need some help:
int a;
int b;
int sumPlayer;
int sumDealer;
char hit = 'h', stand = 's';
char reply;
vector<int> cardsDeck;
int cardDist (int, vector<int>);
int dealerStart ( vector <int>);
int playerTotals( int , vector<int>);
int dealerTotal( int , vector<int>);
int main()
{
/* initialize random seed: */
srand ( time(NULL) );
//TASK: Generates and deals cards for player and dealer
//ACCEPTS: No input
//RETURNS: Cards values of both dealer & player, win or loss comparison
int cardDist( int i, vector<int> cardsDeck )
{
cardsDeck.push_back(rand()% 11 + 1);
cardsDeck.push_back(rand()% 11 + 1);
cout << "Players first 2 cards: ";
for(int i = 0; i < cardsDeck.size(); i++)
{
sumPlayer+=cardsDeck[i];
cout << setw(2) << cardsDeck[i] << "|";
}
cout << endl << endl << "Your 2 card total is: " << sumPlayer << endl << endl;
}
//TASK: Deals 2 initial cards to the dealer
//ACCEPTS: Nothing!
//RETURNS: dealers cards and total
int dealerStart( vector<int> cardsDeck )
{
int sumDealer = 0;
int sum;
for(int p = 0; p < cardsDeck.size(); p++)
{
sumDealer+=cardsDeck[p];
cout << setw(2) << cardsDeck[p] << "|";
}
cout << endl << endl << "Dealer has a total of: " << sumDealer << endl << endl;
sumDealer = sum;
//return sumDealer;
} // PASSING sumDealer FROM HERE TO THE LAST FUNCTION
//TASK: To take input from player regarding "HIT" or "STAND"
//ACCEPTS: Input for "HIT" or "STAND"
//RETURNS: Adittional cards to player with hand total
int playerTotals( int sumPlayer, vector<int> cardsDeck )
{
int c, d, e;
int loop1, loop2, loop3;
cout << "Would you like to HIT or STAND? ";
cin >> reply;
cin.ignore(256, '\n');
cout << endl;
if (reply == stand )
{
cout << endl << "You have chosen to stand with your current hand total. " << endl;
}
else if ( reply == hit )
{
cardsDeck.push_back(rand()% 11 + 1);
loop1 = sumPlayer;
for(int i = 0; i < cardsDeck.size(); i++)
{
loop1+=cardsDeck[i];
cout << setw(2) << cardsDeck[i];
c = i;
}
cout << endl << "your card is: " << cardsDeck[c] << " and your hand total is: " << loop1 << endl;
if ( loop1 > 21)
{
cout << "BUST!!! You have more than 21" << endl;
}
else if ( sumPlayer < 21 )
{
cout << "Would you like to HIT or STAND? ";
cin >> reply;
cin.ignore(256, '\n');
if (reply == stand )
{
cout << endl << "You have chosen to stand with your current hand total. " << endl;
}
else if ( reply == hit )
{
cardsDeck.push_back(rand()% 11 + 1);
loop2 = sumPlayer;
for(int x = 0; x < cardsDeck.size(); x++)
{
loop2+=cardsDeck[x];
cout << setw(2) << cardsDeck[x];
d = x;
}
cout << endl << "your card is: " << cardsDeck[d] << " and your hand total is: " << loop2 << endl;
if ( loop2 > 21)
{
cout << "BUST!!! You have more than 21" << endl;
}
else if ( sumPlayer < 21 )
{
cout << "Would you like to HIT or STAND? ";
cin >> reply;
cin.ignore(256, '\n');
if (reply == stand )
{
cout << endl << "You have chosen to stand with your current hand total. " << endl;
}
else if ( reply == hit )
{
cardsDeck.push_back(rand()% 11 + 1);
loop3 = sumPlayer;
for(int y = 0; y < cardsDeck.size(); y++)
{
loop3+=cardsDeck[y];
cout << setw(2) << cardsDeck[y];
e = y;
}
cout << endl << "your card is: " << cardsDeck[e] << " and your hand total is: " << loop3 << endl;
cout << "you have reached a maximum of 5 cards with a total of: " << loop3 << endl;
if ( sumPlayer > 21)
{
cout << "BUST!!! You have more than 21" << endl;
}
}
}
}
else if (reply == stand )
{
cout << "You have chosen to stand with your current hand total. " << endl;
}
}
}
else if ( reply == stand )
{
cout << "You have chosen to stand with your current hand total. " << endl;
}
}
int dealerTotal( int sum, vector<int> cardsDeck) //TRYING TO PASS sumDealer//BUT NOT GETTING CORRECT VALUE
{
int sumDealer = sum;
cout << endl << "dealer has: " << sum << endl << endl;