0

The error seems to be related to pointers but I can't quite pinpoint what is going on. I'll split the card array into type and suit later but for now I'd like to figure out what is going on with the error, I tried making the parameters of swap pointers with no real success. I'd greatly appreciate any help.

//in functions.cpp
#include "Header.h"

void randomizer()
{
    srand(time(NULL));
}

void swap(string a, string b)
{
    string temp = a;
    a = b;
    b = temp;
}

string Card::generateRandomCard()
{
    randomizer();
    int random = rand() % 52;
    cout << "Your draw is: " << card[random] << endl;
    return card[random];
}

void Deck::randomize(string arr[], int size)
{
    randomizer();
    for (int i = size - 1; i > 0; i--)
    {
        int j = rand() % (i + 1);

        swap(arr[i], arr[j]);
    }
}

void Deck::printDeck()
{
    randomizer();
    Card card1;
    cards[52] = card1.card[52];
    randomize(cards, 52);
    for (int i = 0; i < 52; i++)
    {
        cout << cards[i] << endl;
    }
    cout << "\nYour hand: " << cards[51] << endl << cards[50] << endl << cards[49] << endl << cards[48] << endl << cards[47] << endl;
    cout << "The next card in the deck is: " << cards[48] << endl;
}

//in Header.h
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

void randomizer();
void swap(string a, string b);

class Card
{
public:
    string generateRandomCard();
    const string card[52] = { "2 of Clubs", "3 of Clubs", "4 of Clubs", "5 of Clubs", "6 of Clubs", "7 of Clubs", "8 of Clubs", "9 of Clubs", "10 of Clubs", "J of Clubs", "Q of Clubs", "K of Clubs", "A of Clubs",
        "2 of Spades", "3 of Spades", "4 of Spades", "5 of Spades", "6 of Spades", "7 of Spades", "8 of Spades", "9 of Spades", "10 of Spades", "J of Spades", "Q of Spades", "K of Spades", "A of Spades",
        "2 of Hearts", "3 of Hearts", "4 of Hearts", "5 of Hearts", "6 of Hearts", "7 of Hearts", "8 of Hearts", "9 of Hearts", "10 of Hearts", "J of Hearts", "Q of Hearts", "K of Hearts", "A of Hearts",
        "2 of Diamonds", "3 of Diamonds", "4 of Diamonds", "5 of Diamonds", "6 of Diamonds", "7 of Diamonds", "8 of Diamonds", "9 of Diamonds", "10 of Diamonds", "J of Diamonds", "Q of Diamonds", "K of Diamonds", "A of Diamonds" };
private:
};

class Deck
{
public:
    void randomize(string arr[], int size);
    void printDeck();
private:
    string cards[52];
};

//in main.cpp
#include "Header.h"

void main()
{
    Card card1;
    Deck mainDeck;
    mainDeck.printDeck();
    card1.generateRandomCard();
}
Deneb
  • 3
  • 1
  • 2
  • 1
    [OT]: call `srand(time(NULL));` only once. – Jarod42 Mar 03 '17 at 02:33
  • `void swap(string a, string b)` is mostly useless. References are missing. – Jarod42 Mar 03 '17 at 02:33
  • 2
    `cards[52] = card1.card[52];` doesn't do what you expect, it does out of bound accesses. it would be `cards = card1.card` to copy array content if you have used `std::array` or `std::vector` instead of C-arrays. – Jarod42 Mar 03 '17 at 02:36
  • 2
    1) Learn to use a debugger. 2) `#include "Header.h"`? Really? What happens when you need a second header file? `#include "Header2.h"`? – Ken White Mar 03 '17 at 02:41
  • @KenWhite The assignment states I should name the Header file like that, not up to me. – Deneb Mar 03 '17 at 02:46
  • @Jarod42 you were absolutely right. Thank you very much. – Deneb Mar 03 '17 at 03:06

1 Answers1

1

0xCCCCCCCC is a special value stored in uninitialized pointers by Microsoft Visual Studio. The code you posted contains an out-of-bound access, when you do:

cards[52] = card1.card[52];

The size of the array is 52, but arrays are indexed by offset, so the offset 52 would land in the 53th element, which is non-existent.

You could try to debug the code, step line by line, and discover when the error is throw.

Kahler
  • 1,130
  • 6
  • 24
  • 1
    [When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](http://stackoverflow.com/q/370195/995714) – phuclv Mar 03 '17 at 02:41
  • @LưuVĩnhPhúc Nice! – Kahler Mar 03 '17 at 02:42
  • @Kahler I suppose it has an error reading the string when it comes into the Card object which is the first object called in main. – Deneb Mar 03 '17 at 02:58
  • @Kahler that being said, thank you for pointing out the error with cards[52], you saved me there :) – Deneb Mar 03 '17 at 03:07