I am trying to put either an X or an O in an array. It doesn’t seem to work, however. It says, “Run-Time Check Failure #2 – Stack around the variable ‘row1’ was corrupted.”
char row1[19];
char row2[19];
char row3[19];
char row4[19];
for (int i = 0; i < 20; i++)
{
int r = int(((double) rand() / (RAND_MAX)) + 1);
if (r == 0)
{
row1[i] = 'X';
}
else
{
row1[i] = 'O';
}
}
cout << row1[0] << endl;
How can I generate a random X or O? Thank you.
As elmigranto stated: your loop is incorrect. It should be:
That is because
char row4[19];is an array that contains 19 elements. The first element isrow4[0]and the last isrow4[18]because the numbering is starts from 0. So in the last looprow[19]will cause an error.