Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8759949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:55:50+00:00 2026-06-13T14:55:50+00:00

I am trying to code a game of Blackjack. I have been self-learning C++

  • 0

I am trying to code a game of Blackjack. I have been self-learning C++ in my spare time, and this is my first time posting on any website about programming.

I have been looking up the answers to my problems as I go along, and have learned quite a lot.. but this problem has completely baffled me. I fear that I am approaching the task completely wrong, and hopefully you can help me with this.

I have a Card class, and a Deck class that holds a vector of 52 Cards. The vector is a private member of Deck class, and I fear this is my problem?

When I add the random_shuffle line to my code, it compiles fine but then the console window crashes (Windows 7 x64, code::blocks, c++). I can’t work out what I am doing wrong. I call the vector random access iterators begin() and end()…

deck.h

#ifndef DECK_H
#define DECK_H

#include <vector>

using namespace std;

/** Card Class */
class Card
{
public:
/** Constructor prototypes */
//Card(); //default constructor
Card(int s, int r) : suit(s), rank(r) {} 

/** GET function prototypes */
int getRank(); // returns card number as int
string getSuit(); // returns the suit in a string

private:
int rank;
int suit;
} ;

/** Deck class */
class Deck
{
public:
Deck();
vector <Card> get_deck() { return deck; };

private:
vector<Card> deck;
};

#endif // DECK_H

deck.cpp

#include <iostream>
#include <string>
#include <vector>
#include "deck.h"

using namespace std;

/** Deck ctor to initialise deck */
Deck::Deck()
{
for(int suit = 0; suit < 4; suit++)
{
    for(int rank = 0; rank < 13; rank++)
    {
        deck.push_back(Card(suit,rank));
    }
}

}

/** Functions to GET rank and suit */
// Function to get rank as int
int Card::getRank()
{
return rank;
}

// Function to get suit as string
string Card::getSuit()
{
switch(suit)
{
    case 0:
    return "Diamonds";

    case 1:
    return "Hearts";

    case 2:
    return "Clubs";

    case 3:
    return "Spades";

    default:
    return "Error";
}
}

main.cpp

#include <iostream>
#include <algorithm>
#include <ctime> // time()
#include <string>
#include <vector>

#include "deck.h"

using namespace std;

int main()
{

Deck mydeck;

random_shuffle( mydeck.get_deck().begin(), mydeck.get_deck().end() );

// Loop to iterate through deck of cards
for(int i = 0; i<52; i++)
{
    cout << mydeck.get_deck()[i].getRank() << " of " << mydeck.get_deck()[i].getSuit() << endl;
}

// Display size of deck
//cout << endl << "The size of deck is: " << mydeck.get_deck().size() << endl;


return 0;
}

Any help or words of wisdom will be much appreciated, and I hope I formatted everything right…

Many Thanks

Dan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:55:52+00:00Added an answer on June 13, 2026 at 2:55 pm

    This accessor method:

    vector <Card> get_deck() { return deck; };
    

    Returns a copy of the vector of cards. So when you call it twice, you get two different copies, and the begin() of the first copy doesn’t match up with the end() of the second copy, so it crashes.

    To fix it, you should return the array by reference so that a copy is not made:

    vector <Card>& get_deck() { return deck; }  // no semicolon needed here
    //           ^
    //           |
    //    this is a reference
    

    However, this allows the caller to modify the internal array, which is generally a bad idea. To avoid this, you should return it by const reference:

    const vector <Card>& get_deck() { return deck; }
    

    But if you do that, then std::random_shuffle can’t modify the array. So to fix that, the ideal solution would be to add a class method to the Deck class which calls random_shuffle on itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I am trying to code a Connect4 game. For this, I have created
I am trying to code a global lookup table of sorts. I have game
So I've been trying to code a tabletop game that I made a long
I am currently trying to make a game in Android. I have this piece
I am trying to create my first game for android. I currently have a
I've been trying to make a simple blackjack game in python and I seem
Ok, so in creating a game in my spare time, I've been completely stuck
I've been trying to figure out this problem. I have an assignment to make
I'm trying to code some stuff for a game but I keep getting error
trying to refactor code to provide clean association A GAME has a HOME_TEAM and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.