i am trying to compare an element in an array but it gets stuck in a loop. from what i see it doesnt even compare the element. I want to place a x in an array but if there already is an x there i want to place it above it.

#include <iostream>
using namespace std;
const int row = 6;
const int col = 7;
void player (char playgrid [row][col]);
int main()
{
char playgrid[row][col];
for (int currentrow = 0; currentrow <= row; currentrow++)
 {
 for (int currentcol = 0; currentcol <= col; currentcol++)
  playgrid [currentrow][currentcol] = '.';
 } 
 
playgrid [6][2] = 'x';
player (playgrid);
for (int currentrow = 0; currentrow <= row; currentrow++)
 {
 for (int currentcol = 0; currentcol <= col; currentcol++)
  cout << playgrid [currentrow][currentcol];
  
 cout << endl;
 } 

return 0;
}
void player (char playgrid [row][col])
{
 int colselect;
 int rowselect = 6;
 
 cout << "Enter column select: ";
 cin >> colselect;
 
 bool entered = false;
 
 while (entered != true)
 {
 if (playgrid [rowselect][colselect] != 'x')
  {
  playgrid [rowselect][colselect] = 'x';
  entered = true;
  }
  
 rowselect -= 1
 ;
  
 
 }

 
 
}

Recommended Answers

All 2 Replies

lines 9, 11, 17 and 19 -- they are looping too many times, causing data overflow. Replace "<=" with just "<".

line 16: accessing a row in the array that does not exist. Since array elements begin with 0, there is no 6th row.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.