Implementation of Memory game in C++ - keep out of range at memory location error : <p>So, I keep getting an error saying: Unhandled exception at _______ in ProgCW.exe: Microsoft C++ exception: std::out_of_range at memory location ________.</p>

<p>I have found this code online and have implemented it, but there seems to be something wrong with my implementation.</p>

<p>I have used a class to do this.</p>

<p>This is the code:</p>

<pre><code>dispGrid::dispGrid(const int gRows, const int gCols){

//declaration and allocation for creating a grid and cardstatus
std::vector&lt;std::vector&lt;int&gt;&gt; cGrid;

int** cardStatus = new int*[gRows];
for (int i = 0; i &lt; gRows; i++) {
    cardStatus[i] = new int[gCols];
}

//initialisation of variables
int r1, c1, r2, c2;
int moves;
bool gameOver = false;
cardShuffle(cGrid, gRows, gCols);

while (gameOver != true) { //allows to repeat over the game

    moves = 0;

    ///////////////////////////////
    //      prints grid         //
    /////////////////////////////

    //prints the coordinates 
    std::cout &lt;&lt; "\t    ";
    for (int nCols = 0; nCols &lt; gCols; nCols++) {
        std::cout &lt;&lt; nCols + 1 &lt;&lt; " ";
    }

    std::cout &lt;&lt; std::endl;


    //initialisation
    //displays the starred grid according to difficulty level the user chose and sets cardstatus as false, for face down
    for (int nRows = 0; nRows &lt; gRows; nRows++) {
        std::cout &lt;&lt; "\t" &lt;&lt; nRows + 1 &lt;&lt; " | ";
        for (int nCols = 0; nCols &lt; gCols; nCols++) {
            std::cout &lt;&lt; "* ";
            cardStatus[nRows][nCols] = false;
        }
        std::cout &lt;&lt; std::endl;
    }

    /******************************/
    /*      game mechanics       */
    /*****************************/

    do { //allows execution of thr input as long the card status is not false at that card

        //user input for the first card's coordinates
        std::cout &lt;&lt; "Enter the first cards row: " &lt;&lt; std::endl;
        std::cin &gt;&gt; r1;

        std::cout &lt;&lt; "Enter the first cards column: " &lt;&lt; std::endl;
        std::cin &gt;&gt; c1;

        if ((r1 &amp;&amp; c1) == 0) { //checks to see whether user has input the values 0
            std::cout &lt;&lt; "#NOTE : There are no coordinates of that value." &lt;&lt; std::endl;
        }

    } while (cardStatus[r1 - 1][c1 - 1] != false);

    do { //allows execution of thr input as long the card status is not false at that card

         //user input for the second card's coordinates
        std::cout &lt;&lt; "Enter the second cards row: " &lt;&lt; std::endl;
        std::cin &gt;&gt; r2;

        std::cout &lt;&lt; "Enter the second cards column: " &lt;&lt; std::endl;
        std::cin &gt;&gt; c2;

        if ((r2 &amp;&amp; c2) == 0) {//checks to see whether user has input the values 0
            std::cout &lt;&lt; "#NOTE : There are no coordinates of that value." &lt;&lt; std::endl;
        } 
        else if (cardStatus[r2 - 1][c2 - 1] = true) { //if the cardstatus of the second card is true then output the care is already flipped
            std::cout &lt;&lt; "#NOTE : This card is already flipped." &lt;&lt; std::endl;
        }
        else if ((r2 == r1) &amp;&amp; (c2 == c1)) { //if the cards are the same then the card is already flipped
            std::cout &lt;&lt; "#NOTE : You have already chosen this card." &lt;&lt; std::endl;
            continue;
        }
    } while (cardStatus[r2 - 1][c2 - 1] != false);

    r1--;
    c1--;
    r2--;
    c2--;

    //reaveal cards
    //prints the coordinates 
    std::cout &lt;&lt; "\t    ";
    for (int nCols = 0; nCols &lt; gCols; nCols++) {
        std::cout &lt;&lt; nCols + 1 &lt;&lt; " ";
    }

    std::cout &lt;&lt; std::endl;


    //initialisation
    //displays grid with blank spaces according to the coordinates then picked, setting up to replace blank spaces for values
    for (int nRows = 0; nRows &lt; gRows; nRows++) {
        std::cout &lt;&lt; "\t" &lt;&lt; nRows + 1 &lt;&lt; " | ";
        for (int nCols = 0; nCols &lt; gCols; nCols++) {
            if ((nRows == r1) &amp; (nCols == c1)) {
                std::cout &lt;&lt; cGrid[nRows][nCols] &lt;&lt; " ";
            }
            else if ((nRows == r2) &amp;&amp; (nCols == c2)) {
                std::cout &lt;&lt; cGrid[nRows][nCols] &lt;&lt; " ";
            }
            else if (cardStatus[nRows][nCols] = true) {
                std::cout &lt;&lt; cGrid[nRows][nCols] &lt;&lt; " ";
            }
            else {
                std::cout &lt;&lt; "* ";
            }

        }
        std::cout &lt;&lt; std::endl;
    }

    //if match?
    if (cGrid[r1][c1] == cGrid[r2][c2]) {
        std::cout &lt;&lt; "#NOTE : Cards Match!" &lt;&lt; std::endl;

        cardStatus[r1][c1] = true;
        cardStatus[r2][c2] = true;
    }
    std::cin.get();
    std::cin.get();


    /*********************************************************/
    /*               prints to blank screen                  */
    /*********************************************************/

    for (int z = 0; z &lt;= 30; z++) {
        std::cout &lt;&lt; std::endl;
    }

    //reprint the board
    //prints the coordinates 
    std::cout &lt;&lt; "\t    ";
    for (int nCols = 0; nCols &lt; gCols; nCols++) {
        std::cout &lt;&lt; nCols + 1 &lt;&lt; " ";
    }

    std::cout &lt;&lt; std::endl;


    //initialisation
    //displays the starred grid according to difficulty level the user chose and sets cardstatus as false, for face down
    for (int nRows = 0; nRows &lt; gRows; nRows++) {
        std::cout &lt;&lt; "\t" &lt;&lt; nRows + 1 &lt;&lt; " | ";
        for (int nCols = 0; nCols &lt; gCols; nCols++) {
            if (cardStatus[nRows][nCols] = true) {
                std::cout &lt;&lt; cGrid[nRows][nCols] &lt;&lt; " ";
            }
            else {
                std::cout &lt;&lt; "* ";
            }

        }
        std::cout &lt;&lt; std::endl;
    }
    gameOver = true;

    //checks all card status, should all be set to true 
    for (int nRows = 0; nRows &lt; gRows; nRows++) {
        for (int nCols = 0; nCols &lt; gCols; nCols++) {
            if (cardStatus[nRows][nCols] == false) {
                gameOver = false;
                break;
            }
        }
        if (gameOver == false) {
            break;
        }
    }
    moves++; //counts moves
}

std::cout &lt;&lt; "#NOTE : You have matched all the tiles" &lt;&lt; std::endl;
std::cout &lt;&lt; "#NOTE: You had " &lt;&lt; moves &lt;&lt; "moves." &lt;&lt; std::endl &lt;&lt; std::endl; }  
</code></pre>

<p>The problem seems to be when the cards need to be shuffled.</p>

<p>The function for shuffling the cards:</p>

<pre><code>void dispGrid::cardShuffle(std::vector&lt;std::vector&lt;int&gt;&gt; &amp;gCards, int rows, int cols)//parameters of a 2d vector and the number of rows and columns {

int numOfElem = rows*cols;//total number of random number should be made
std::vector&lt;int&gt; randNum;

//generate same random numbers
for (int x = 0; x &lt; (numOfElem / 2); x++) {
    const int fNum = rand() % 100 + 1; //generate num between 1 and 100
    int sNum;
    do {
        sNum = rand() % 100 + 1;
    } while (fNum == sNum);
    randNum.push_back(fNum);
    randNum.push_back(sNum);
}

for (int s = 0; s &lt;= 20; s++) {
    for (int x = 0; x &lt; numOfElem; x++) {
        srand((unsigned)time(NULL));//initialise random seed
        int i = rand() % (numOfElem - 1) + 1; //chooses a number between the length of the array
        int temp = randNum.at(x); //sets temp as the value in the at that indes in the start array
        randNum.at(x) = randNum.at(i); //sets the value at that index in the start array as the value of the index at the start array 
        randNum.at(i) = temp;
    }
}

int i = 0;

for (int nRows = 0; nRows &lt; rows; nRows++) {// for every row and column
    for (int nCols = 0; nCols &lt; cols; nCols++) {
        gCards.at(nRows).at(nCols) = randNum.at(i);//card at that coordinate will be equal to 
        std::cout &lt;&lt; gCards[nRows][nCols];
        i = i + 1; 
    }
    std::cout &lt;&lt; std::endl;
}}
</code></pre>

<p>I am using Microsoft Visual Studio, and when I put the breakpoint in, the problem was appearing at the point:</p>

<pre><code>gCards.at(nRows).at(nCols) = randNum.at(i); //card at that coordinate will be equal to 
</code></pre>

<p>Many thanks for the help.</p>
