DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   changing array value... (http://www.daniweb.com/forums/thread220910.html)

gopi17 Sep 5th, 2009 3:07 pm
changing array value...
 
ok...i'm having trouble changing a value in a array into a character
void gameon(int **game,int &size,int &x,int &y,int &score1,int &score2)
{
  int i,j,num1,num2;
  cout<<"Please enter a number from the selected row/column -->";
  cin>>num1;
    for(j=0;j<size;j++)
    {
        if(num1==game[x][j])
        {
          cout<<"Number found"<<endl;
          game[x][j]=='X';
          score1=score1+num1;
          y=j;
        }
    }
    display(game,size,score1,score2);
}

i can't seem to change that specific number into 'X'..
can u guys help me out...

vmanes Sep 5th, 2009 6:57 pm
Re: changing array value...
 
Two problems in one statement.
game[x][j]=='X';

First, you're using the equality test ( == ) instead of assignment ( = ).
Second, you're trying to assign a character to an integer variable. This will work (when you fix problem 1) but will not give you the result I think you're expecting. What will be assigned to the array element is the ASCII value of 'X' (which is 88, if I read my ASCII chart right.)

If you want to mark the spot as used or free or whatever, you'll need to pick a value out of the range of allowable values. For instance, if the valid data can be any positive number greater than 0 (zero), use 0. If 0 could be a valid data item, how about some negative number? If all possible numbers are potentially valid, then you need some other way to indicate the cell is empty/available or whatever. Perhaps a parallel array where you simply mark 0 or 1 to indicate that the corresponding cell in the data array is available or not.


All times are GMT -4. The time now is 11:41 am.

Forum system based on vBulletin Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©2003 - 2010 DaniWeb® LLC