Hi guys and gals, 1st post here! Woot!
With that out of the way, I need some help with this steaming fresh BJ program. I need to write functions for the yes deal and no deal options, but the player total seems to get messed up somehow and when a new card is added after the fist two, the total becomes the second card + the first card. I need a little help figuring out what would be the problem.

Code is here:

``````#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include "drawmore"
using namespace std;

int main()
{
//blackjack
int you, deal, toty, totd, hold, yhold;
char opto, agn;

unsigned seed = time(0);
srand(seed);
do
{
toty = 0;
totd = 0;
hold = 0;
you = 0;
deal = 0;
yhold = 0;

cout << "\n\n\n\n\nWeclome to C++ Blackjack!\n\n";
you = 1 + rand() % 10;
cout << "Your first card is: " << you << endl;
toty += you;
you = 1 + rand() % 10;
cout << "Your next card is: " << you << endl;
toty += you;
cout << endl;

deal = 1 + rand() % 10;
hold = deal;
cout << "Your dealers first card is: " << deal << endl;
totd += deal;
deal = 1 + rand() % 10;
cout << "Your dealers next card is hidden! " << endl;
totd += deal;

yhold = toty;

cout << "\nYour current total is: " << toty << endl;
cout << "The house's current total is: " << hold << " plus a hidden card!" <<  endl;

for ( ; toty < 21;)
{
cout << "Deal? (Y/N) ";
cin >> opto;

if (opto == 'N' || opto == 'n')
{
if (toty > totd || toty == totd)
{
cout << "You win!\n"<< toty << " is more than or equal to " << totd << endl;
break;
}
else
{
cout << "The house wins!\n" << totd << " is more than " << toty << endl;
break;

}
}

if (opto == 'y' || opto == 'Y')
{
drawmore (you, toty);
}
cout << "Play again? (Y/N) ";
cin >> agn;
}while (agn == 'Y' || agn == 'y');

return 0;
}``````

And the function looks like this:

``````#include <iostream>
using namespace std;

void drawmore (int you, int toty)
{
you = 1 + rand() % 10;
cout << "Your next card is: " << you << endl;
toty += you;
if (toty == 21)
{
cout << "21! You win!";
break;
}
else if (toty < 21 && toty > 1)
{
cout << "Your total is: " << toty << endl;
}
else
{
cout << toty << " = Bust! You lose!" << endl;
break;
}

opto = 0;
}``````
Used code tags correctly on very first post :)
2
Contributors
1
2
Views
7 Years
Discussion Span
Last Post by Lerner

Welcome to the board and thanks for using code tags!

In order for the value of toty in main() to retain the change done to it in drawmore(), you need to send it to drawmore() by reference instead of by value. Change the declaration of drawmore() appropriately.

Not that you asked but:

I don't see why you need yhold or hold at all.

You appear to be missing a closing brace for the for loop in main().

drawmore() needs toty and totd from main(). You can declare the version of you that drawmore() uses to be local to drawmore().

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.