| | |
Type problems writing to an array
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 4
Reputation:
Solved Threads: 0
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
The pointer refers to the protected char **ptr
Board.h
Any help would be appreciated, thanks.
Board.cpp
C++ Syntax (Toggle Plain Text)
void Board::create() { ptr = new char *[width]; for (int i=0;i<width;i++) { *(ptr + i) = new char[height]; } int k = 1; for (int i = 0; i < width; i++) { for (int j = 0; j<height;j++) { *(*(ptr+i)+j) = k++; } } }
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;
};•
•
Join Date: Jul 2005
Posts: 1,874
Reputation:
Solved Threads: 298
C++ Syntax (Toggle Plain Text)
ptr = new char *[width]; for (int i = 0; i < width; i++) { ptr[i] = new char[height]; }
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
•
•
Join Date: May 2008
Posts: 662
Reputation:
Solved Threads: 111
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.
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)
// ptr is just a char * in my class void Board::create() { ptr = new char [ width * height ]; // filling the cells with numbers (I don't know why) int k = 1; for (int i = 0; i < width; i++) { for (int j = 0; j<height;j++) { ptr[i * width + j] = k++; } } }
Last edited by Murtan; Dec 10th, 2008 at 12:30 am. Reason: wasn't finished with the code
![]() |
Similar Threads
- Tutorial: Understanding ASP classes (ASP)
- Differences Between Java and C/C++ (C++)
- fstream Tutorial (C++)
- Problems in Address book programming (C++)
- I have Problems with Airline Reservations System, A bucket sort and a Turtle Graphic (C++)
- Array Problems (C++)
- array problems (C++)
- Parse XML from ASP!!! (ASP)
- Algorithm Efficiency Help!!!! (C)
- HELP: class static function - compile errors (C++)
Other Threads in the C++ Forum
- Previous Thread: Opening a file
- Next Thread: lost downstream!
Views: 415 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm api array arrays assignment basic beginner binary borland browser c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count delete dll dynamic encryption error file files forms fstream function functions game givemetehcodez graph gui homework http iamthwee input int lazy lib link linker list loop loops map math matrix memory network newbie news number object objects operator output parameter pointer pointers problem program programming project random read recursion recursive reference sort spoonfeeding string strings struct student studio system template templates text time tree variable vc++ vector video visual visualstudio win32 window windows winsock wordfrequency






