Question: Why isnt this code running? I belive itd have to due with the saveResultsToFile at the end of the play game. Ive been trying to
Why isnt this code running?
I belive itd have to due with the "saveResultsToFile" at the end of the play game. Ive been trying to make multiple, CardGame.h CardGame.cpp main.cpp and a README.txt which isnt already included since im not able to make mutiples files and get the code running. if you could explain the process of which ive shown im trying to get to id need id appreciate it
#include
#include
#include
#include
#include
#include
#include
class CardGame
private:
std::vector deck;
int userScore;
int dealerScore;
public:
CardGame;
void shuffleDeck;
int drawCard;
void playGame;
void saveResultsToFileconst std::string &filename;
;
Constructor to initialize the game
CardGame::CardGame : userScore dealerScore
Initialize the deck with values from to representing card values
forint i ; i ; i
deck.pushbacki;
deck.pushbacki;
Shuffle the deck using a random generator
void CardGame::shuffleDeck
std::randomdevice rd;
std::mt grd;
std::shuffledeckbegin deck.end g;
Function to draw a card from the deck
int CardGame::drawCard
ifdeckempty
std::cout "The deck is empty!" std::endl;
return ;
int card deck.back;
deck.popback;
return card;
Main game logic
void CardGame::playGame
shuffleDeck;
users turn
std::cout "Your turn!" std::endl;
char choice;
whileuserScore
int card drawCard;
userScore card;
std::cout "You drew a card with value: card std::endl;
std::cout "Your current score: userScore std::endl;
ifuserScore
std::cout "exceeded You lose!" std::endl;
return;
std::cout Do you want to draw another card? yn: ;
std::cin choice;
ifchoice y
break;
Dealers turn
std::cout "Dealer's turn!" std::endl;
whiledealerScore
int card drawCard;
dealerScore card;
std::cout "Dealer drew a card with value: card std::endl;
std::cout "Dealer's current score: dealerScore std:: endl;
ifdealerScore
std::cout "dealer exceed you win!" std::endl;
return;
Determine winner
ifuserScore dealerScore
std::cout "Congradulation! You win!" std::endl;
else ifuserScore dealerScore
std::cout "It's a tie!" std::endl;
else
std::cout "Dealer wins! Better luck next time." std::endl;
Save game results to a file
void CardGame::saveResultsToFileconst std::string &filename
std::ofstream filefilename;
iffileisopen
file "Game results:
;
file "User Score: userScore
;
file "Dealer Score: dealerScore
;
file.close;
std::cout "Results saved to filename std::endl;
else
std::cout "Failed to open file!" std::endl;
int main
CardGame game; Create an instance of the game
game.playGame; Play game
game.saveResultsToFilegameresults.txt; save results to a file
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
