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++;
}
}
}