i got this code from the FAQ and i tried adopting it to my code i already had but i cant get it to work.
basically what i need to do is i made a grid using ascii characters and i have to have a cursor move about that grid but my program always crashes when it gets to that part cause i know i did something wrong there
here is all the code i have so far
Code:
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <stdio.h>
#include "Definitions.h"
using namespace std;
enum
{
KEY_ESC = 27,
ARROW_UP = 256 + 72,
ARROW_DOWN = 256 + 80,
ARROW_LEFT = 256 + 75,
ARROW_RIGHT = 256 + 77
};
static int get_code ( void )
{
int ch = getch();
if ( ch == 0 || ch == 224 )
ch = 256 + getch();
return ch;
}
class Darray{
protected:
int DEPTH,
ROW,
COL;
int*** array;
public:
Darray();
Darray(int depth, int row, int col);
~Darray();
Darray(const Darray& darray); //copy constructor
Darray& Darray::operator= (const Darray& darray); //assignment operator
void getDimensions(int DEPTH, int ROW, int COL);
void destroy();
void create();
void init();
void resetSize(int DEPTH, int ROW, int COL);
};
Darray::Darray()
{
DEPTH=0;
ROW=0;
COL=0;
array=NULL;
}
void Darray::create()
{
array = new int** [DEPTH];
for(int d=0; d<DEPTH; d++) {
*(array+d) = new int* [ROW];
for (int r=0; r<ROW; r++)
*(*(array+d)+r) = new int [COL];
}
}
Darray::Darray(int depth, int row, int col)
{
DEPTH = depth;
ROW = row;
COL = col;
create();
init();
}
Darray::~Darray()
{
destroy();
}
Darray::Darray(const Darray& darray)
{
DEPTH = darray.DEPTH;
ROW = darray.ROW;
COL = darray.COL;
create();
for(int d=0; d<DEPTH; d++)
for(int r=0; r<ROW; r++)
for(int c=0; c<COL; c++)
array[d][r][c] = darray.array[d][r][c];
}
Darray& Darray::operator= (const Darray& darray)
{
resetSize(darray.DEPTH, darray.ROW, darray.COL);
for(int d=0; d<DEPTH; d++)
for(int r=0; r<ROW; r++)
for(int c=0; c<COL; c++)
array[d][r][c] = darray.array[d][r][c];
return *this;
}
void Darray::destroy()
{
for (int d=0; d<DEPTH; d++)
{
for(int r=0; r<ROW; r++)
delete [] *(*(array+d)+r);
delete [] *(array+d);
}
delete [] array;
}
void Darray::init()
{
for(int d=0; d<DEPTH; d++)
for(int r=0; r<ROW; r++)
for(int c=0; c<COL; c++)
array[d][r][c] = 0;
}
void Darray::getDimensions(int DEPTH, int ROW, int COL)
{
cout << "The Depth is: " << DEPTH << " The Row is: " << ROW
<< " The Columns is: " << COL << endl;
}
void Darray::resetSize(int depth, int row, int col)
{
Darray temp (*this);
destroy();
DEPTH=depth;
ROW=row;
COL=col;
create();
init();
}
/***********************************************************************************/
class Grid : public Darray {
private:
int currentRow,
currentDepth,
currentCol;
public:
Grid();
void arrow_keys();
void UsFunction();
void DsFunction();
void clear_da_screen(void);
void print () const;
};
Grid::Grid()
{
int depth,
row,
col;
cout << "Enter the grid dimensions: " << endl;
cin >> depth >> row >> col;
// set up dimensions
DEPTH = depth;
ROW = row;
COL = col;
// set up position
currentRow = ROW / 2 + 1;
currentDepth = DEPTH / 2 + 1;
currentCol = COL / 2 + 1;
create();
}
void Grid::print () const
{
// makes grid
//makes top row or grid
for(int d=0; d<DEPTH; d++)
{
if(d<DEPTH)
{
cout << U_L;
cout << LINE;
for(int c=0; c<COL-1; c++)
{
cout << T_M;
cout << LINE;
}// end for
cout << U_R;
cout << "\n";
for( c=0; c<COL+1; c++)
{
cout << VERT_LINE;
cout << " ";
}// end vertical line first row
}cout << "\n";// end top of grid
// makes gird middle
for(int r=0; r<ROW-1; r++)
if(r<ROW)
{
cout << L_SM;
cout << LINE;
for(int c=0; c<COL-1; c++)
{
cout << M;
cout << LINE;
}// end for
cout << R_SM;
cout << "\n";
for( c=0; c<COL+1; c++)
{
cout << VERT_LINE;
cout << " ";
}cout << endl;
}// end middle of grid
// makes last line
if(d<DEPTH)
{
cout << L_L;
cout << LINE;
for(int c=0; c<COL-1; c++)
{
cout << L_MT;
cout << LINE;
}
cout << L_R;
}
cout << endl;
}//end last row
}
void Grid::UsFunction()
{
resetSize(DEPTH, ROW, COL);
for(int d=0; d<DEPTH; d++)
for(int r=0; r<ROW; r++)
for(int c=0; c<COL; c++)
{
array[d][r][c] = array[currentDepth][currentRow][currentCol];
}
}
void Grid::DsFunction()
{
resetSize(DEPTH, ROW, COL);
for(int d=0; d<DEPTH; d++)
for(int r=0; r<ROW; r++)
for(int c=0; c<COL; c++)
{
array[d][r][c] = array[currentDepth][currentRow][currentCol];
}
}
void Grid::arrow_keys()
{
array[DEPTH][ROW][COL];
for(int d=0; d<DEPTH; d++)
{
for(int r=0; r<ROW; r++)
{
for(int c=0; c<COL; c++)
{
array[d][r][c]=' ';
}
}
}
print();
int d,r,c;
d=0;
r=0;
c=0;
int ch;
while ( ( ch = get_code() ) != KEY_ESC ) {
// clear_da_screen();
switch ( ch )
{
case ARROW_UP:
//printf ( "UP\n" );
c=c-1;
break;
case ARROW_DOWN:
//printf ( "DOWN\n" );
c=c+1;
break;
case ARROW_LEFT:
//printf ( "LEFT\n" );
r=r-1;
break;
case ARROW_RIGHT:
//printf ( "RIGHT\n" );
r=r+1;
break;
}
array[c][r][d]=ACTIVE;
for(int d=0; d<DEPTH; d++)
{
for(int r=0; r<ROW; r++)
{
for(int c=0; c<COL; c++)
{
cout<<array[d][r][c]<<"";
}
}cout<<"\n";
}
array[c][r][d]=VISIT;
//break;
}
cin.get();
cin.get();
}
/*void Grid::clear_da_screen(void)
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '),
dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}
*/
/******************************************************************************/
class Cell{
private:
bool visit,
active;
public:
Cell();
~Cell();
void fill();
};
Cell::Cell()
{
visit = 0;
active = 0;
}
Cell::~Cell()
{
}
//fill cell when visited
void Cell::fill()
{
}
/*******************************************************************************/
void main()
{
//Darray d;
Grid g;
Cell c;
//d.getDimensions(depth, row, col);
g.arrow_keys();
g.print();
}
here is my definitions file:
Code:
#define M char (197);
#define U_L char (218);
#define L_L char (192);
#define U_R char (191);
#define L_R char (217);
#define T_M char (194);
#define L_MT char (193);
#define L_SM char (195);
#define R_SM char (180);
#define VERT_LINE char (179);
#define VISIT char (219);
#define LINE char (196);
#define ACTIVE char (248);
what is it that im doing wrong i have never done this before