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
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:
You should have defined rowcol as:
Hope this helps!
Keith