954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Comparing an element in an array

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

 
 
}
keatsey-9
Newbie Poster
5 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

http://www.daniweb.com/techtalkforums/announcement8-3.html

Isn't the watermark at the back of the edit box enough of a clue?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You