tags:

views:

326

answers:

4

Hey guys, I am trying to practice C++ and while doing so I ran into a problem in my code. I dynamically create a character array and then for each array index, I want to fill that element with an integer. I tried casting the integer to a character but that didn't seem to work. After printing out the array element, nothing comes out. I would appreciate any help, I'm pretty new to this, thanks .

char *createBoard()
{   
    char *theGameBoard = new char[8];    
    for (int i = 0; i < 8; i++)    
        theGameBoard[i] = (char)i;    //doesn't work

    return theGameBoard;    
}

Here is how I ended up doing it.

char *createBoard()    
{    
    char *theGameBoard = new char[8];    
    theGameBoard[0] = '0';    
    theGameBoard[1] = '1';    
    theGameBoard[2] = '2';    
    theGameBoard[3] = '3';    
    theGameBoard[4] = '4';    
    theGameBoard[5] = '5';    
    theGameBoard[6] = '6';    
    theGameBoard[7] = '7';    
    theGameBoard[8] = '8';    

    return theGameBoard;    
}
+14  A: 

Basically, your two sections of code are not quite equivalent.

When you set theGameBoard[0] = '0' you are essentially setting it to the value 48 (the ASCII code for the character '0'). So setting theGameBoard[0] = (char)i is not quite the same thing if i = 0. You need to add the offset of '0' in the ASCII table (which is 48) so that theGameBoard[0] is actually 0 + the offset of character '0'.

Here's how you do it:

 char *createBoard()
 {
      char *theGameBoard = new char[8];
      for (int i = 0; i < 8; i++)
           theGameBoard[i] = '0' + (char)i; //you want to set each array cell
                                            // to an ASCII numnber (so use '0' as an offset)

      return theGameBoard;    
 }

Also, like @Daniel said: make sure that you free up the memory that you are allocating in this function after you are done with using the returned variable. Like so:

int main()
{ 
    char* gameBoard = createBoard();

    // you can now use the gameBoard variable here
    //     ...

    // when you are done with it
    // make sure to call delete on it
    delete[] gameBoard;

    // exit the program here..
    return 0;
}
Miky Dinescu
If you don't want to cast the `i` in `int`, you can declare it as a char: for( char i=0; i<8; i++) theGameBord[i] = '0'+i;
ThibThib
You are mixing the use of new and free. This is bad. You need to use delete [].
anio
@mamin you are correct. fixed it (thanks for pointing it out)
Miky Dinescu
OP's second snippet also has a buffer overflow.
Adam Rosenfield
yea I didn't paste the delete[] part. Thanks!!
Kap Choi
+2  A: 

If I was doing this I would use stringstream. It might be heavy weight for this, but it is the C++ way of doing things.

for (int i = 0; i < 8; ++i) {
     stringstream sstream;
     sstream << i;
     sstream >> theGameBoard[i];
    }

When you are done using the game board array you need to delete it with this command:

delete[] theGameBoard;
anio
Fair enough, but at least factor the sstream-variable out of the loop, output each number to it and then read the entire string. No need for creating and destroying eight stringstream-objects just for this.
Magnus Hoff
A: 

In your character array you must store ASCII values of digits.

For example: ASCII value of '0' is 48 ( not 0 ) ASCII value of '1' is 49 ( not 1 ) ...

In C++ ( and almost every other language ) you can get ASCII value of character putting it in single quote ( '0' == 48 , '1' == 49 , '2' == 50 , ... )

Your array must have values

theGameBoard[0] = '0' 
theGameBoard[1] = '1' or theGameBoard = '0' + 1

...

Code that fills your array:

for(int k=0;k<8;++k)
     theGameBoard[k] = '0' + k;
dpetek
+3  A: 

Your second function has an off-by-one bug. You allocate an array of length 8, but you copy 9 values into it. (0, 1, 2, 3, 4, 5, 6, 7, 8).

SPWorley
oops yea I noticed that afterwards. Thanks!!!
Kap Choi