Type problems writing to an array

Thread Solved

Join Date: Sep 2008
Posts: 4
Reputation: chasee is an unknown quantity at this point 
Solved Threads: 0
chasee chasee is offline Offline
Newbie Poster

Type problems writing to an array

 
0
  #1
Dec 9th, 2008
I'm trying to create an array that can be resized, and automatically renumbered, at the moment, what I have works for up to a 3x3 array, but anything larger than that doesn't work. I've tried doing this with int instead of char for the type or the array, but then I'm not able to write a character to the board, only the ascii numerical equivalent.
Any help would be appreciated, thanks.

Board.cpp
  1. void Board::create()
  2. {
  3. ptr = new char *[width];
  4.  
  5. for (int i=0;i<width;i++)
  6. {
  7. *(ptr + i) = new char[height];
  8. }
  9.  
  10. int k = 1;
  11. for (int i = 0; i < width; i++)
  12. {
  13. for (int j = 0; j<height;j++)
  14. {
  15. *(*(ptr+i)+j) = k++;
  16. }
  17. }
  18. }
The pointer refers to the protected char **ptr

Board.h
class Board
{
    public:
    void create();
    void display();
    void end();
    void set_height(int);
    void set_width(int);
    void write();

    protected:
    int height, width;
    char **ptr;
    char Player;
};
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,874
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 298
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Type problems writing to an array

 
0
  #2
Dec 9th, 2008
  1. ptr = new char *[width];
  2.  
  3. for (int i = 0; i < width; i++)
  4. {
  5. ptr[i] = new char[height];
  6. }

Code for a 2 dimensional array of char that could be sized to any width and height would stop there. The rest of what you have looks like you would use if you were trying for a 3 dimensional (cube, box, whatever instead of board) array. That is, if you could retain your sanity with all the detractions of the dereferences and pointer math.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 662
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 111
Murtan Murtan is offline Offline
Practically a Master Poster

Re: Type problems writing to an array

 
0
  #3
Dec 10th, 2008
Another common option is to leave the board in a single dimensional array. Depending on how you interface to the board, it has advantages and disadvantages.

For example, a 3 x 3 board would use 9 elements in an array. The first 3 would be the first row (or first column depending on your preference) the next 3 would be the second row and the last 3 would be the third and final row.

  1. // ptr is just a char * in my class
  2. void Board::create()
  3. {
  4. ptr = new char [ width * height ];
  5.  
  6. // filling the cells with numbers (I don't know why)
  7. int k = 1;
  8. for (int i = 0; i < width; i++)
  9. {
  10. for (int j = 0; j<height;j++)
  11. {
  12. ptr[i * width + j] = k++;
  13. }
  14. }
  15. }
Last edited by Murtan; Dec 10th, 2008 at 12:30 am. Reason: wasn't finished with the code
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 662
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 111
Murtan Murtan is offline Offline
Practically a Master Poster

Re: Type problems writing to an array

 
0
  #4
Dec 10th, 2008
For more help with the int vs char for the array implementation, it would help to have a better idea of how the board will be used.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 4
Reputation: chasee is an unknown quantity at this point 
Solved Threads: 0
chasee chasee is offline Offline
Newbie Poster

Re: Type problems writing to an array

 
0
  #5
Dec 10th, 2008
Nah, that worked, thanks.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 415 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC