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();
}