#### Archived

This topic is now archived and is closed to further replies.

# C++ blackjack help

This topic is 5025 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I made a blackjack game but there is one problem. No matter what you type the first time, it gives a "hit". All the other times it works. here is the code:
#include <iostream>
#include <time.h>

using namespace std;
int main() {

string action;
int youo;
int yout;
int deao;
int deat;
int youn;
int dean;
string what;

do {

cout << "Welcome to blackjack!!!!!!!!!!!!!!\n\nType start to start :) :  ";
cin >> action;

} while (action == "");

do {

srand(time(0));

youo = 1 + rand() % 11;
yout = 1 + rand() % 11;
deao = 1 + rand() % 11;
deat = 1 + rand() % 11;
addim = youo + yout;
adddm = deao + deat;

cout << "You got a " << youo << " and a " << yout <<".  The dealer's face up card is " << deao;
cout << "\n\nDo you hit or stay?:  ";
cin >> action;

} while (action == "start");

do {

youn = 1 + rand() % 11;

if (adddm < 15) {
dean = 1 + rand() % 11;
what = "hit";
}
else
{
what = "stayed";
}

if (addim > 21) {

cout << "You got over 21, you lose!\n\n";
cout << "Type q to quit, start to go again";
cin >> action;

}

else if (adddm > 21) {

cout << "The dealer got over 21, you win!\n\n";
cout << "Type q to quit, start to go again";
cin >> action;

}

else if (addim == 21) {

cout << "You got 21, you win!\n\n";
cout << "Type q to quit, start to go again";
cin >> action;

}
else if (adddm == 21) {

cout << "The dealer got 21, you lose!\n\n";
cout << "Type q to quit, start to go again";
cin >> action;

} else {

cout << "Your new card is " << youn << ".  You have a combined card total of " << addim << ".  The dealer " << what;
cout << "\n\nDo you hit or stay?:  ";
cin >> action;

}

} while (action == "hit");

do {

if (adddm < 15) {
dean = 1 + rand() % 11;
what = "hit";
}
else
{
what = "stayed";
}

if (addim > 21) {

cout << "You got over 21, you lose!\n\n";
cout << "Type q to quit, start to go again";
cin >> action;

}

else if (adddm > 21) {

cout << "The dealer got over 21, you win!\n\n";
cout << "Type q to quit, start to go again";
cin >> action;

}

else if (addim == 21) {

cout << "You got 21, you win!\n\n";
cout << "Type q to quit, start to go again";
cin >> action;

}
else if (adddm == 21) {

cout << "The dealer got 21, you lose!\n\n";
cout << "Type q to quit, start to go again";
cin >> action;

}

cout << "The dealer got a higher score than you, you lose!\n\n";
cout << "Type q to quit, start to go again";
cin >> action;

}

cout << "You got a higher score than the dealer, you win!\n\n";
cout << "Type q to quit, start to go again";
cin >> action;

}

} while (action == "stay");

return 0;

}

[edited by - mpfeif101 on April 16, 2004 1:01:17 PM]

##### Share on other sites
Try declaring action before you use it first? It doesn''t look buggy as is but you never know.

Also, you''re naming scheme is atrocious.

When you find yourself in the company of a halfling and an ill-tempered Dragon, remember, you do not have to outrun the Dragon...

##### Share on other sites
I did (or at least I think I did) declare action.

string action;

And how can I make my naming scheme not atrocious?

I''m a big newb at C++, constructive criticism is appreciated,

##### Share on other sites
you really should organize stuff into functions...

while(askdjfklajsdlkfa){  if (kajjdfklajdsklfaj)    function(1);}

etc.

aksdjfklajsdklfjas

##### Share on other sites
Hmmm, alright. But how can I make it so that the first time you input something it works?

In case I didn''t explain it, when you first start off by typing "start", it works. The time after that it goes to hit no matter what you type. After that, it works perfectly, if you type hit it hits you and if you type stay you stay. Any ideas?

##### Share on other sites
Okay, first change the condition in the first do-while loop from while (action == ""); to while (action != "start"); That way, your program wont start until they type start, instead of just typing anything. Then move the "do" part of the second do-while loop down right before those couts, so it'll just repeat the text, but not the action. Also, change the condition from while (action == "start"); to while (action != "hit" && action != "stay"); Now it wont break out of the loop without you typing "hit" or "stay". The next do-while loop needs to be replaced with just a normal while loop. And the last do-while loop should just be an if statement. The end result should be like this:
#include <iostream>#include <string>#include <time.h>using namespace std;int main(){	string action;	int youo;	int yout;	int deao;	int deat;	int youn;	int dean;	int addim;	int adddm;	string what;	do	{		cout << "Welcome to blackjack!!!!!!!!!!!!!!\n\nType start to start :) :  ";				cin >> action;	}	while (action != "start");			srand(time(0));			youo = 1 + rand() % 11;	yout = 1 + rand() % 11;	deao = 1 + rand() % 11;	deat = 1 + rand() % 11;	addim = youo + yout;	adddm = deao + deat;			do	{		cout << "You got a " << youo << " and a " << yout <<".  The dealer's face up card is " << deao;		cout << "\n\nDo you hit or stay?:  ";		cin >> action;	}	while (action != "hit" && action != "stay");		while (action == "hit")	{		youn = 1 + rand() % 11;		addim = youn + addim;		if (adddm < 15)		{			dean = 1 + rand() % 11;			adddm = dean + adddm;			what = "hit";		}		else		{			what = "stayed";		}				if (addim > 21)		{			cout << "You got over 21, you lose!\n\n";			cout << "Type q to quit, start to go again";			cin >> action;					}		else if (adddm > 21)		{			cout << "The dealer got over 21, you win!\n\n";			cout << "Type q to quit, start to go again";			cin >> action;		}		else if (addim == 21)		{			cout << "You got 21, you win!\n\n";			cout << "Type q to quit, start to go again";			cin >> action;		}		else if (adddm == 21)		{			cout << "The dealer got 21, you lose!\n\n";			cout << "Type q to quit, start to go again";			cin >> action;		}		else		{			cout << "Your new card is " << youn << ".  You have a combined card total of " << addim << ".  The dealer " << what;			cout << "\n\nDo you hit or stay?:  ";			cin >> action;		}	}			if (action == "stay")	{		if (adddm < 15)		{			dean = 1 + rand() % 11;			adddm = dean + adddm;			what = "hit";		}		else		{			what = "stayed";		}				if (addim > 21)		{			cout << "You got over 21, you lose!\n\n";			cout << "Type q to quit, start to go again";			cin >> action;		}		else if (adddm > 21)		{			cout << "The dealer got over 21, you win!\n\n";			cout << "Type q to quit, start to go again";			cin >> action;		}		else if (addim == 21)		{			cout << "You got 21, you win!\n\n";			cout << "Type q to quit, start to go again";			cin >> action;		}		else if (adddm == 21)		{			cout << "The dealer got 21, you lose!\n\n";			cout << "Type q to quit, start to go again";			cin >> action;		}		else if (adddm > addim)		{			cout << "The dealer got a higher score than you, you lose!\n\n";			cout << "Type q to quit, start to go again";			cin >> action;		}		else if (adddm < addim)		{			cout << "You got a higher score than the dealer, you win!\n\n";			cout << "Type q to quit, start to go again";			cin >> action;		}	}			return 0;}

There's other bugs, but I don't wanna rewrite your whole program.

I agree with the other posters that your variable names aren't very good. A decent variable name is one where anyone should be able to guess what it is for. Like the variable "adddm" should be something like "dealerTotal".

Caeddyn

[edited by - Caeddyn on April 16, 2004 6:30:01 PM]

##### Share on other sites
Caeddyn, thanks so much for your help .

A couple questions about what you did though....

1) Why did you include string? (#include <string>)
2) Why not consistent?

For example, how come for hit you go while (action == "hit") { but for stay you go if (action == "stay") {

3) You said there are other bugs, as this is my first actual program (if you call this a program) besides a calcuator in C++, please tell me so I can learn

4) Does the variable name affect a program? Like the variable names make sense to me, so do I have to change them?

[edited by - mpfeif101 on April 16, 2004 7:57:38 PM]

##### Share on other sites
To Q4>
No, but as it is it's tough on the eyes

[edited by - Drakkcon on April 17, 2004 1:21:27 AM]

##### Share on other sites
1) I had to #include <string> because that''s the header file for the class string. I added it after my compiler yelled at me that string wasn''t defined. I guess your compiler already includes it, and that''s why you weren''t getting any errors.

2) Well, you might hit multiple times, so that needs to be a loop. When you stand though, you''ll only do that once.

3) The other problems should be pretty easy for you to find. Like one is your program says, "Type q to quit, start to go again". Obviously, that''s not been implemented. Also, the dealer stuff should be separated from the player. The dealer might need to take like 5 cards, but if the player stays right away, he won''t get any.

Programming is a constant cycle of breaking and fixing things. So, you can''t just keep posting "What''s wrong with this?", you''re going to have to find and fix your own problems yourself.

4) You can name your variables whatever you want (except when they''re already being used by something else). You could have them all start off with "Bob", followed by a number. But, when you come up with variable names, they should (not must) be easy to understand what that variable is for without even looking at the code. That way, if you worked for some company, and 10 years later after you''re long gone some poor sod is trying to figure out what your code does, it''ll be a little bit easier for them to do that.

Also, when showing code to others, it''s best to put in some comments. Though, like having good variable names, they''re not required.

Caeddyn

##### Share on other sites
Ok. Thanks for all your help

-Matt