Hi, I was wondering what was wrong with the following code. It creates a vector <int> and assigns it the values 0 - 51. Then it shuffles these values and prints it to the screen. When I compile it, I get no errors, but when it runs, it throws an error on Windows and it quits the console window and tells me that it has encountered an error and needed to closed. Any ideas what's wrong?

#include <vector>
#include <iostream>
using namespace std;


// Globals
vector <int> deck;


// Shuffle deck
void shuffleDeck() {
	vector <int> shuffled;
	int s = shuffled.size();
	int decksize = deck.size();
	int rindex;

	while( s < decksize ) {
		rindex = ( rand() % deck.size() );
		shuffled.push_back( deck[ rindex ] );
		deck.erase( deck.begin() + rindex );
		s = shuffled.size();
	}

	deck = shuffled;
}


// Main
int main() {
	// Set deck as 0 through 51
	for( int i = 0; i < 52; i ++ )
		deck[ i ] = i;

	// Shuffle
	shuffleDeck();

	// Show deck
	vector <int>::iterator vit;
	for( vit = deck.begin(); vit != deck.end(); vit ++ )
		cout << *(vit) << endl;

	cin.get();
}

lines 13 and 14: the size of the vectors is 0 because you have not assigned any elements to the vectors. Therefore the loop starting on line 17 will never execute because both s and decksize are 0.

similar lines 31 and 32 cause program to crash because deck does not have a size.

How to fix: before line 31 assign a size: deck.resize(52); Even despite the above problems that shuffle function doesn't work because rand() often returns the same value, so the shuffled deck will contain duplicate values. What you need to do is keep track of the values returned by rand(). Each time it generates a value, check the previous values to see if the new value has already been generated. If it has, then generate another number until it finds a unique number.

You also need to delete line 20 because erasing the contents of the vector willl also cause the program to crash.

Oh! Wow that was a stupid mistake. I didn't realize I had been trying to use deck[ i ] = i; on the vector without a length specified. Another option would be to replace line 32 with deck.push_back( i ); , right?
(I'm new to how vectors work in C++, obviously :P)

yes that will work too. Please re-read my previous post because I added more.

I don't understand that part you added to your post... I eliminated the possibility of rand() duplicating cards in the deck by erasing each index rand() selected from the deck. The range for rand() each time is changed because it gets the new size of the deck (after the previous rindex was erased). At the end, the deck vector is assigned the value of the shuffled vector. This has been working for me so far; no crashes.

Does your way of doing it have something to do with more deep working of C++, and my way will lead to future issues that aren't apparent to me? Because as of now, it works.

Works for me too. I keep getting the same shuffle of the deck because I didn't seed the random number generator before calling rand(), but I have had no problems with erasure of element from vector using either VC6++ or VC8Express.

Ok great, sounds like everything's solved for me. Thanks everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.