Couple of logical errors-Blackjack

Here's the blackjack game I wrote.
Any help is greatly appreciated.

So one problem with the game is if both players choose to stand the program won't end and will just tell the players what their scores are infinitely.

Another problem, the game is not writing to the file what the results are at the end of the game.

Thanks.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188`` `````` /* */ //Libraries #include #include #include #include #include using namespace std; //Global Constants int score, block; int pickedCard; string winner, answer; int activePlayer = 0; const int playerSize = 2; string name[playerSize]; bool endGame = false; const int size = 52; int cardDeck[size] = {1,2,3,4,5,6,7,8,9,10,11,12,13, 1,2,3,4,5,6,7,8,9,10,11,12,13, 1,2,3,4,5,6,7,8,9,10,11,12,13, 1,2,3,4,5,6,7,8,9,10,11,12,13}; bool usedCard[size]; int totalScore[playerSize] = {0, 0}; //Function Prototypes void game(); int deal(); void nameFunction(); void playAgain(); void saveResult(string); void swapPlayer(); void checkWin(); void handlePlayer(); int drawCard(bool, const int); //Begin Execution int main(){ cout << "Welcome to blackjack" << endl; nameFunction(); endGame = false; while (endGame==false) { game(); } return 0; } //main function runs the game void game(){ for (int i=0; i> answer; if(answer == "H"||answer=="h") { pickedCard = deal(); cout << "Dealer has given " << name[activePlayer] << " a card worth " << pickedCard << " points." << endl; cout << endl; } checkWin(); swapPlayer(); } //function shuffles cards int deal() { block = rand()%51; if(usedCard[block] == true) { deal(); } else { score = cardDeck[block]; totalScore[activePlayer]+= score; usedCard[block] = true; } return score; } //function for alternating players void swapPlayer() { if(activePlayer==0) { activePlayer=1; } else { activePlayer=0; } } //function to find a winner void checkWin() { if(totalScore[0] > 21) { cout << name[0] << " bust over 21." << endl; cout << name[1] << "has won!" << endl; winner = name[1]; saveResult(winner); endGame = true; cout << "play again?(Y/N)?"; cin >> answer; if (answer=="Y"||answer=="y") { playAgain(); } else { cout << "thanks for playing" << endl; } } if(totalScore[1] > 21) { cout << name[1] << "over 21 bust!." << endl; cout << name[0] << " has won!" << endl; winner = name[1]; saveResult(winner); endGame = true; cout << "play again? (Y/N)?"; cin >> answer; if (answer=="Y"||answer=="y") { playAgain(); } else { cout << "thank you for playing" << endl; } } } void playAgain() { totalScore[0] = 0; totalScore[1] = 0; endGame = false; swapPlayer(); game(); } //writes results to a file void saveResult(string winner) { ofstream file; file.open ("result.txt", ios::app); file << winner << " has won" << endl; file.close(); } ``````
Last edited on
Global constants are OK, but you really need to avoid global variables.

As far loop when both players "stand" you need to rethink your logic in `checkWin()`
Yeah my professor isn't too fond of those either. I was gonna remove them after I got the errors fixed.

Okay I'll get to checking that.

Anything for the read and write to the file failing?

Thank You.
Topic archived. No new replies allowed.