"Stack around the variable 'board' was corrupted" error in this code. But, why!?!
This is a simple Tic Tac Toe game that i'm programming just to review some C++ concepts.
Code:
# include <iostream>
# include <string>
# include <iomanip>
using namespace std;
const int ROWS = 2;
const int COLS = 2;
void showBoard ();
int main () {
string playerOneName = "";
string playerTwoName = "";
cout << "Player 1, enter your name: ";
cin >> playerOneName;
system ("cls");
cout << "Player 2, enter your name: ";
cin >> playerTwoName;
system ("cls");
cout << "\a\aPlayer 1 name is -> " << playerOneName << endl;
cout << "Player 2 name is -> " << playerTwoName << endl;
system ("pause");
showBoard();
system ("pause");
return 0;
}
void showBoard ()
{
int selection = 0;
int rSelection = 0;
int cSelection = 0;
cout << "Here is your board!" << endl;
char board [ROWS][COLS];
for (int c = 0; c <= ROWS; c++)
{
cout << setw (10);
for (int c2 = 0; c2 <= COLS; c2++)
{
board [c] [c2] = '*';
cout << board [c] [c2];
}
cout << endl;
}
cout << "Press 1 for instructions or 2 for start to play right now!";
cout << "1 or 2? -> ";
cin >> selection;
if (selection == 1)
cout <<"Do nothing" ;
else if (selection == 2)
system ("cls");
cout << "Enter the number of the Row that you want the ""X"" : ";
cin >> rSelection;
cout << "Enter the number of the Column that you want the ""X"" : ";
cin >> cSelection;
rSelection -= 1;
cSelection -= 1;
board [rSelection][cSelection] = 'x';
for (int c = 0; c <= ROWS; c++)
{
cout << setw (10);
for (int c2 = 0; c2 <= COLS; c2++)
{
cout << board [c] [c2];
}
cout << endl;
}
}
This is just the beginning and this code is not even near of what it should be, I know. HOWEVER, this code suppose to write an "x" in the board, the user indicate the row and the column that he wants. Why then it trows this exception?? -> "Stack around the variable 'board' was corrupted" If I don't break the debugging and I continue, it put the "x" where it should be. I'm missing something?? Please help!!