0

When running the following code, I am attempting to update a Tic Tac Toe game board. When you type in 3 as a column, it sets 2 X's or O's in the game board.

Here is an example of the output

*  *  *
*  *  *
*  *  *

X: Select a Row: 1
X: Select a Col: 3
*  *  X
X  *  *
*  *  *

Here is the desired output

*  *  *
*  *  *
*  *  *

X: Select a Row: 1
X: Select a Col: 3
*  *  X
*  *  *
*  *  *

Here is the code

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int rowSelect = 0;
    int colSelect = 0;
    char turn = 'X';
    char rowcol[2][2];

    for(int i=0; i < 3; i++)
    {
        for(int j=0; j < 3; j++)
        {
            rowcol[i][j] = '*';
        }
    }

    for(int i=0; i < 3; i++)
    {
        for(int j=0; j < 3; j++)
        {
            cout << rowcol[i][j] << "  ";
        }
        cout << endl;
    }

    cout << endl;

    while (true)
    {

        cout << turn << ": Select a Row: ";
        cin >> rowSelect;
        while (rowSelect < 1 || rowSelect > 3)
        {
            cout << "I cannot accept that value, try again!" << endl;
            cout << endl;
            cout << turn << ": Select a Row: ";
            cin >> rowSelect;
        }

    cout << turn << ": Select a Col: ";
    cin >> colSelect;
    while (colSelect < 1 || colSelect > 3)
    {
        cout << "I cannot accept that value, try again!" << endl;
        cout << endl;
        cout << turn << ": Select a Col: " << endl;
        cin >> colSelect;
    }

    rowcol[rowSelect-1][colSelect-1] = turn;

    if (turn == 'X')
    {
        turn = 'O';
    }
    else
    {
        turn = 'X';
    }

        for(int i=0; i < 3; i++)
        {
            for(int j=0; j < 3; j++)
            {
                cout << rowcol[i][j] << "  ";
            }
            cout << endl;
        }

    }

    system("PAUSE");
    return 0;
}

Thanks!

-Mike

4 Answers 4

4

The problem is the array. Although arrays are accessed using zero based indices, the definition requires the actual number of elements for which to reserve space.

You defined rowcol as:

char rowcol[2][2];  // This defines a 2 x 2 array

You should have defined rowcol as:

 char rowcol[3][3];  // This defines a 3 x 3 array

Hope this helps!

Keith

Sign up to request clarification or add additional context in comments.

Comments

3

Your rowcol array needs to be 3x3:

char rowcol[3][3];

Comments

0
char rowcol[2][2];

In all the cases, i, j must iterate only until < 2 since it is a 2x2 array.

Comments

0
  • Your array only holds 2 elements per row, while your loop runs through three rows and three columns. You seem to be confused on how arrays are numbered, an array with 2 elements would be accessed using elements[0] and elements[1], because 0 is the first number in programming(not 1). you need to declare an array of THREE elements, and access them using [0] [1] and [2].

FIX: change to char Array[3][3];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.