0

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
 ;
  
 
 }

 
 
}
3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by Ancient Dragon
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.