943,948 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 448
  • C++ RSS
Dec 9th, 2008
0

Type problems writing to an array

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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;
};
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chasee is offline Offline
4 posts
since Sep 2008
Dec 9th, 2008
0

Re: Type problems writing to an array

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Dec 10th, 2008
0

Re: Type problems writing to an array

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.

c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 10th, 2008
0

Re: Type problems writing to an array

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 10th, 2008
0

Re: Type problems writing to an array

Nah, that worked, thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chasee is offline Offline
4 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Opening a file
Next Thread in C++ Forum Timeline: lost downstream!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC