-2

Error message: Unhandled exception at 0x00F94619 in Cpluspluslearning.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00CD2FC0).

I am trying to make a deck of playing cards, to learn c++. Everything works fine, and I am trying to make a method for shuffling the deck. I realizes that "rand" needs to be seeded, to be "truly" random. But when I use the "srand" method i recieve the error message stated above. With no srand it works fine, but is not actually random. As this is my first C++ project, I would love some help.

Sincerely, sad person.

Code:

#include <iostream>
#include <algorithm>
#include <ctime>

using namespace std;

//-------------------Classes-----------------------

class Card {
public:
    string suit, color, name;
    int value;

    Card(string x, string y, string q, int z) {
        suit = x;
        color = y;
        value = z;
        name = q;
    }
    Card() {}

    string getSuit() {
        return suit;
    }

    string getColor() {
        return color;
    }

    string getName() {
        return name;
    }

    int getValue() {
        return value
;
    }

    string toString() {
        return name + " of " + suit;
    }

};

//-----------------Variables----------------

Card deck[52];
string suits[] = {"Hearts","Spades","Diamonds","Clubs"};
string color[] = {"Red","Black"};
string name[] = { "Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King" };

//------------------Methods----------------

void createDeck() {

    int count = 0;
    for (int i = 0; i <= 51; i++) {
        
        if (count == 13) { count = 0;}
        
        if (i <= 12) {
            deck[i] = Card(suits[0], color[0], name[count], count + 1);
        }
        else if (i <= 25) {
            deck[i] = Card(suits[1], color[1], name[count], count + 1);
        }
        else if (i <= 38) {
            deck[i] = Card(suits[2], color[0], name[count], count + 1);
        }
        else {
            deck[i] = Card(suits[3], color[1], name[count], count + 1);
        }
        count++;
    }
    
}

int randomNumber(int array[],int size) {
    int random;

    srand(time(NULL));
    random = rand() % 52;
    
    for (int i = 0; i < size; i++) {
        if (array[i] == random) {
            randomNumber(array, size);
        }
    }
    return random;
}

void shuffleDeck() {
    int indexes[52];
    Card shuffledDeck[52];

    for (int i = 0; i <= 51; i++) {
        indexes[i] = randomNumber(indexes, 51);
        
        shuffledDeck[i] = deck[indexes[i]];
    }

    for (int i = 0; i < 51; i++)
    {
        deck[i] = shuffledDeck[i];
        cout << deck[i].toString() + " ";
    }
    
}

void game() {
    createDeck();
    shuffleDeck();
}

//-----------------Main------------

int main() {
    game();
    return 0;
}

1 Answers1

1
indexes[i] = randomNumber(indexes, 51);

should be

indexes[i] = randomNumber(indexes, i);

because the rest of the array is uninitialized so far.

Also the "try again case"

randomNumber(array, size);

needs to be

return randomNumber(array, size);

otherwise you'll end up returning the original duplicate number anyway.

As a bonus, this fix will make the function tail-recursive, which means with a good compiler and tail-call optimization enabled, there will be no stack growth.

In addition, you really want to move the srand(time(NULL)) call out of the loop as was already commented otherwise you generate the exact same random number a whole bunch of times before the computer clock ticks and gives you a different time(NULL) value.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720