0

Like my title says I am having an issue with getting an out range error with my code in C++. The basic gist is that I am making a vector for a 52 card deck. After which I am randomizing the order of the cards.

When I go to print out the cards one by one in the terminal I get an out of range error for the location of memory. The output gives me the first card but then breaks.

From error checking I know my Deck size is 1 but I thought that my push back when creating the deck would increase the deck size to be 52.

Is there something I am missing? Any help is appreciated.

#include <iostream>
#include <chrono>       
#include <random>
#include <vector>

int main() {
    srand(time(0));
    std::vector <std::string> Deck[52];
    CreateDeck(Deck);
    ShuffleDeck(Deck);
    ShowDeck(Deck);
}

void ShowDeck(std::vector <std::string> Deck[52]) {
    for (size_t i = 0; i < 52; i++) {

        // Microsoft C++ exception: std::out_of_range at memory location 0x004FF718.
        std::cout << Deck->at(i) << ",";
        if ((i + 1) % 13 == 0) {
            std::cout << "\n" << std::endl;
        }
    }
}

void ShuffleDeck(std::vector <std::string> Deck[52]) {

    //shuffle deck to a randomize order
    unsigned seed = rand() % 100;
    std::shuffle(Deck, Deck + 52, std::default_random_engine(seed));
    std::cout << " Shuffling Deck......." << std::endl;
}

void CreateDeck(std::vector <std::string> Deck[52])
{
    //using the arrays below contruct a 52 playing card deck 
    std::string suit[4] = { "S","C","D","H" };
    std::string value[13] = { "A","2","3","4","5","6","7","8","9","10","J","Q","K" };
    int x = 0;
    for (size_t j = 0; j < 4; j++)
    {
        for (size_t i = 0; i < 13; i++)
        {
            Deck[x].push_back("[" + value[i] + suit[j] + "]");
            x++;
        }
    }
}
Nexusred
  • 3
  • 2
  • 4
    `std::vector Deck[52];` is an array of 52 *vectors*. If you want a vector with 52 strings, use `std::vector deck(52);` or `std::vector deck; deck.resize(52);`. – François Andrieux Mar 03 '20 at 20:37

1 Answers1

2

You needed to get rid of the arrays and use references. A vector is a resizable array already. And the reference means to use an alias to the original instead of making a copy. If you had removed the array [52] bits, but not used references, then it would have copied the array into each function, meaning your original Deck in main would not have been updated.

Plus, I had to move main to the end. I don't know why MSVC would have let you use main at the top, but the code in main requires that the other functions be declared first, and they were at the bottom so they weren't seen.

#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
#include <vector>

void ShowDeck(const std::vector<std::string> &Deck) {
  for (size_t i = 0; i < 52; i++) {

    // Microsoft C++ exception: std::out_of_range at memory location 0x004FF718.
    std::cout << Deck.at(i) << ",";
    if ((i + 1) % 13 == 0) {
      std::cout << std::endl;
    }
  }
}

void ShuffleDeck(std::vector<std::string> &Deck) {

  // shuffle deck to a randomize order
  unsigned seed = rand() % 100;
  std::shuffle(Deck.begin(), Deck.begin() + 52,
               std::default_random_engine(seed));
  std::cout << " Shuffling Deck......." << std::endl;
}

void CreateDeck(std::vector<std::string> &Deck) {
  // using the arrays below contruct a 52 playing card deck
  std::string suit[4] = {"S", "C", "D", "H"};
  std::string value[13] = {"A", "2", "3",  "4", "5", "6", "7",
                           "8", "9", "10", "J", "Q", "K"};
  int x = 0;
  for (size_t j = 0; j < 4; j++) {
    for (size_t i = 0; i < 13; i++) {
      Deck.push_back("[" + value[i] + suit[j] + "]");
      x++;
    }
  }
}

int main() {
  srand(time(0));
  std::vector<std::string> Deck;
  CreateDeck(Deck);
  ShuffleDeck(Deck);
  ShowDeck(Deck);
}
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • the `x` that was used as index for the array can be removed in `CreateDeck` and `srand(time(0));` is also not needed here – 463035818_is_not_an_ai Mar 03 '20 at 20:56
  • Ok I think I under stand what you mean. So my original code was sending empty copies of my array called Deck. And using those references to the memory of my array Deck would take the changes I made into account since I am altering the memory itself correct? – Nexusred Mar 03 '20 at 21:00
  • @Nexusred Your original code sort of worked without references because C++, like C before it, passes arrays as pointers to the first array element. So by using an array of 52 separate vectors you were using pointers to your array in `main` and your functions wrote through the pointer to the `main` Deck array. – Zan Lynx Mar 03 '20 at 23:51