Homework help on dealing cards

Trying to write a code that shuffles a deck of cards and then deals them.
I think I've gotten most of it down, I just have 2 problems.
1) My deck is not shuffling, then when I exit the program, I end up with some error which varies depending on how much I play around with the code. Pretty sure it's only the shuffle function because when not called, no error is given.
2) Is there a way to stop a random number generator from accessing something that has already been posted once in an array? (like in an array containing 1,2,3, randomly generates 2, then it has to choose between 1 or 3)


It's rather long-ish so I'm only including the default constructor+shuffle for the deck of cards, and the main function for now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Card.h"
#include "Deckofcards.h"
#include <iostream>
#include <ctime>
using namespace std;

Deckofcards::Deckofcards () {
	int deck[53];
	for (int i=0; i<53; i++) {deck[i]=i;}
	Card currentCard;
}

void Deckofcards::shuffle () {
	srand(time(0));
	for(int i=1;i<53;i++) {
		int swap1=rand()%52+1;
		int swap2=rand()%52+1;
		deck[swap1]=swap2;
		deck[swap2]=swap1;
	}
}
#include <ctime> 
#include <iostream>
#include <string>
#include "Deckofcards.h"

using namespace std;

int main () {
	srand(time(0));
	Deckofcards moo;
	moo.shuffle();
	for (int i=1;i<53;i++) {
		moo.dealCard(i);
		if (moo.moreCards(i)==false) {cout << "Out of cards.";}
	}
	
}
Last edited on
Were do you declare/define deck? You must have it some where else otherwise shuffle would not compile.
1
2
3
4
5
6
7
8
9
10
11
12
13
Deckofcards::Deckofcards () {
        //-----------------------------------------------
        // This is a local variable, whatever you do to it
        // below will not be saved!!
	int deck[53];       

	for (int i=0; i<53; i++) {deck[i]=i;}

        //-----------------------------------------------
        // This is a local variable, whatever you do to it
        // below will not be saved!!
	Card currentCard;
}

Also, shuffle will never move the zero because of your +1 in int swap1=rand()%52+1;.
I defined Deck in the private section of the Deckofcards class header file. I figured that when the deckofcards Moo was defined, it would use the deck defined in the default constructor since it isn't initialized in any other way.

And I'm aware of that since 52 cards, but array starts at 0, might as well just leave that blank and save myself any complications.
The deck you define in the private section of Deckofcards is not the deck you are modifying in Deckofcards::Deckofcards () constructor. After moo is created the values in private attribute deck are not initialized, the values in the local variable deck in the constructor are. But after you leave the Deckofcards constructor, the deck you initialized is out of scope and gone along with its values.
If you did:
1
2
3
4
5
6
7
8
9
10
Deckofcards::Deckofcards () {	
	for (int i=0; i<53; i++) {
           deck[i]=i;
        }
        
        // This line of code does nothing but run the constructor of 
        // Card and that is it.  Because you don't do anything else 
        // with it, you can just remove it.
	Card currentCard;                                       
}

The values in the private attribute deck would now be set.

Topic archived. No new replies allowed.