Blackjack game

I am wanting to provide the player of my game with an option to play the game again.

This is what I currently have:

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

using namespace std;

int main()
{
srand((unsigned)time(0));

int dealer_card1 = rand() % 13+1;
int dealer_card2 = rand() % 13+1;
int player_card1 = rand() % 13+1;
int player_card2 = rand() % 13+1;
char ans = 'n';

cout << "Welcome to your game of Blackjack!\n";
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 ";
default: cout << dealer_card1 << "and";
break;
}

switch(dealer_card2){
case 1: cout << "Ace and ";
break;
case 11: cout << "Jack and ";
break;
case 12: cout << "Queen and ";
break;
case 13: cout << "King and ";
default: cout << dealer_card2 << "and";
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 !!" << endl;
else
if ((player_card2 == 1) && (player_card1 == 10|11|12|13))
cout << "Blackjack! You Win !!" << endl;
else
if (player_total > dealer_total)
cout << "\n" << "You Win!!!"<< endl;
else
cout << "\n" << "You lose" << endl;

{
cout << "\n" << "Do you want to play again? (y/n): ";
cin >> ans;
while ((ans == 'n') && (ans == 'N'));



}
return 0;
}
Something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main() {
  bool playAgain = true;
  while(playAgain) {

    /* your code here */

    std::cout << "Play again?\n";
    char choice;
    std::cin >> choice;
    if(choice == 'n') {
      playAgain = false;
    }
  }
  return 0;
}
> if ((player_card1 == 1) && (player_card2 == 10|11|12|13))
First of all, this isn't how you compare player_card2 with a range of values.

Then you need to take most of the code in main, and create a function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void playBlackjack()
{
    int dealer_card1 = rand() % 13+1;
    /// etc etc
    if (player_total > dealer_total)
        cout << "\n" << "You Win!!!"<< endl;
    else
       cout << "\n" << "You lose" << endl;
}

int main()
{
    srand((unsigned)time(0));
    playBlackjack();
}


And then it becomes clear and simple to add the loop that fiji885 suggests.
thank you both this code was driving me mad.
player_card2 == 10|11|12|13;

 error: suggest parentheses around comparison in operand of '|' [-Werror=parentheses]
    4 |     return player_card2 == 10|11|12|13;
      |            ~~~~~~~~~~~~~^~~~~



This doesn't even compile if you use some more strict compiler settings. This is one possible solution:
 
player_card2==10 or player_card2==11 or player_card2==12 or player_card2==13;


Topic archived. No new replies allowed.