0

Im tasked with developing a simple blackjack program for a class. The program I have currently compiles and runs but it isnt paying out properly. A blackjack (21) should pay out 1.5*the wager, but it is doing it more than it should. Here is the code I have.

#include <iostream>
#include <cstdlib> 
#include <ctime>
#include <cmath>

using namespace std; 

int main ()

{
	srand((unsigned)time(0));
	cout << "Welcome to Andrew's Blackjack Game! \n";
	int wager;
	cout << "\n" << "Please place your wager: $";
	cin >> wager;	//wager
	int dealer_card1 = rand() % 13 + 1;  //dealer card 1 
	int dealer_card2 = rand() % 13 + 1;	 //dealer card 2
	int player_card1 = rand() % 13 + 1;	 //player card 1
	int player_card2 = rand() % 13 + 1;  //player card 2
	cout << "\n" << "The dealer has "; 
	switch (dealer_card1) {
		case 1: cout << "Ace and "; 
			break;
		case 11: cout << "Jack and "; 
			break;
		case 12: cout << "Queen and ";
			break;
		case 13: cout << "King and "; 
			break;
		default: cout << dealer_card1 << " and ";
			break;
	}
	switch (dealer_card2) {
		case 1: cout << "Ace"; 
			break;
		case 11: cout << "Jack"; 
			break;
		case 12: cout << "Queen"; 
			break;
		case 13: cout << "King"; 
			break;
		default: cout << dealer_card2;
			break;
	}
	cout << "\n" << "\n" << "You have ";
	switch (player_card1){
		case 1: cout << "Ace and "; 
		break;
		case 11: cout << "Jack and "; 
			break;
		case 12: cout << "Queen and "; 
			break;
		case 13: cout << "King and "; 
			break;
		default: cout << player_card1 << " and "; 
			break;
	}
	switch (player_card2){
		case 1: cout << "Ace"; 
			break;
		case 11: cout << "Jack"; 
			break;
		case 12: cout << "Queen"; 
			break;
		case 13: cout << "King"; 
			break;
		default: cout << player_card2;
			break;
	}
	cout << "\n";
	int dealer_total = dealer_card1 + dealer_card2;
	int player_total = player_card1 + player_card2;
	if ((player_card1 == 1) && (player_card2 == 10|11|12|13))
		cout << "Blackjack! You Win $ " << wager*1.5 << "!!" << endl; 
	else 
		if ((player_card2 == 1) && (player_card1 == 10|11|12|13))
			cout << "Blackjack! You Win $ " << wager*1.5 << "!!" << endl;
		else 
			if (player_total > dealer_total)
				cout << "\n" << "You Win $" << wager << "!!" << endl;
			else 
				cout << "\n" << "You lose" << endl;	
	return 0;
}  //main

I know the problem is in my if else statements im just not sure how to make it work. Thanks for the help.

3
Contributors
3
Replies
8
Views
8 Years
Discussion Span
Last Post by spookyfish
1

This may or may not be part of the problem, but it needs to be adressed:

if(player_card2 == 10|11|12|13)

I did the same thing when I was first learning boolean logic and performing tests, and it took me a long time to understand why things are the way they are, but for now just trust me.... you would think the above is correct, but there is a specific way that boolean tests are handled, either in a singular expression, or a stand-alone variable (which yeilds true or false, non-zero and zero respectively)

if(player_card == 10 || player_card == 11 || player_card == 12
        player_card == 13)

     //or in a simplified version:

     if(player_card2 > 9 && player_card < 14)

     //or

     if(player_card >= 10 && player_card <= 13)

So, keep in mind, boolean logic is handled individually per expression, or as a stand alone TRUE/FALSE flag.

Votes + Comments
Helped me solve my problem really quickly!
0

Nvm, just realized I was wrong...

Edit: I believe the answer to this problem is within Clinton's post.

Edited by restrictment: n/a

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.