Hey guys.
Another problem : (

Well you see, i was attempting to make an array that stores char's in a lets say 10 by 10 grid.
Then i can display it
Example:

char array[10][10] = {
    "----------",
    "|          |",
// ...... ext...
    "----------"
};
for(int y = 0; y < 10; y++)
{
    for(int x =0 ; x < 10; x++)
        cout << array[y][x];
    cout << endl;
}
/* In the end it desplays a border around a 9 by 9 space */

That works fine, but i wanted to make a class that could do this at any width and height.
and while your using it you can change the width and height to either cut some off, or add more on.
You can add to the array using PlotPixel function
or even add another array using PlotSpirit function.

here is my code

my graphics class

class Graphics
{
public:
    Graphics();
    ~Graphics();
    
    bool PlotPixel(char,int,int);
    bool PlotSpirit(char**,int,int,int,int);
    void ClearTextArray();
    char** GetTextArray() { return TextArray; }
    int GetWidth() { return Width; }
    int GetHeight() { return Height; }
    void SetWidth(int);
    void SetHeight(int);
private:    
    char **TextArray;
    int Width;
    int Height;
};
Graphics::Graphics()
{
    TextArray = new char[10][10];
    Width = 10;
    Height = 10;
    ClearTextArray();
}
Graphics::~Graphics()
{
    delete TextArray;
    TextArray = new char[0][0];
    Width = 0;
    Height = 0;
}
void Graphics::SetWidth(int width)
{
    int dif = width - Width;
    if(dif < 0) // getting smaller
    {
        char buf[Height][width];
        int y = 0;
        int x = 0;
        for(y = 0; y < Height; y++)
            for(x = 0; x < width; x++)
                buf[y][x] = TextArray[y][x];
        delete TextArray;
        TextArray = new char[Height][width];
        for(y = 0; y < Height; y++)
            for(x = 0; x < width; x++)
                TextArray[y][x] = buf[y][x];
        Width = width;
    }
    else // getting bigger
    {
        char buf[Height][width];
        int y = 0;
        int x = 0;
        for(y = 0; y < Height; y++)
            for(x = 0; x < Width; x++)
                buf[y][x] = TextArray[y][x];
        for(y = 0; y < Height; y++)
            for(x = x; x < width; x++)
                buf[y][x] = 0;
        delete TextArray;
        TextArray = new char[Height][width];
        for(y = 0; y < Height; y++)
            for(x = 0; x < width; x++)
                TextArray[y][x] = buf[y][x];
        Width = width;
    }
}
void Graphics::SetHeight(int height)
{
    int dif = height - Height;
    if(dif < 0) // getting smaller
    {
        char buf[height][Width];
        int y = 0;
        int x = 0;
        for(y = 0; y < height; y++)
            for(x = 0; x < Width; x++)
                buf[y][x] = TextArray[y][x];
        delete TextArray;
        TextArray = new char[height][Width];
        for(y = 0; y < height; y++)
            for(x = 0; x < Width; x++)
                TextArray[y][x] = buf[y][x];
        Height = height;
    }
    else // getting bigger
    {
        char buf[height][Width];
        int y = 0;
        int x = 0;
        for(y = 0; y < Height; y++)
            for(x = 0; x < Width; x++)
                buf[y][x] = TextArray[y][x];
        for(y = 0; y < height; y++)
            for(x = x; x < Width; x++)
                buf[y][x] = 0;
        delete TextArray;
        TextArray = new char[height][Width];
        for(y = 0; y < height; y++)
            for(x = 0; x < Width; x++)
                TextArray[y][x] = buf[y][x];
        Height = height;
    }
}
void Graphics::ClearTextArray()
{
    for(int i = 0; i < Height; i++)
        for(int n = 0; n < Width; n++)
            TextArray[i][n] = '\0';
}
bool Graphics::PlotPixel(char ch,int x,int y)
{
    if(x <= Width && y <= Height)
        if(x >= 0 && y >= 0)
        {
            TextArray[y][x] = ch;
            return true;
        }
    return false;
}
bool Graphics::PlotSpirit(char** array,int StartX,int StartY,int EndX,int EndY)
{
    if(StartX <= Width && StartX >= 0)
        if(EndX <= Width && EndX >= 0)
            if(StartY <= Height && StartY >= 0)
                if(EndY <= Height && EndY >= 0)
                {
                    for(int y = StartY; y < EndY; y++)
                        for(int x = StartX; x < EndX; x++)
                            TextArray[y][x] = array[y][x];
                    return true;
                }
    return false;
}

Can somone tell me either what might be wrong or maybe how i should go about doing this instead.
Because i know how to use one pointer, but 2 pointers never work : S

-------------------------------------------

Error:

Compiler: Default compiler
Building Makefile: "C:\Users\***\Documents\Documents\Programs\C++ Programs\DevC++ Projects\TextGraphics\Makefile.win"
Executing  make...
c:\dev-cpp\bin\make.exe -f "C:\Users\***\Documents\Documents\Programs\C++ Programs\DevC++ Projects\TextGraphics\Makefile.win" all
c:\dev-cpp\bin\g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"   

In file included from main.cpp:2:
Graphics.h: In constructor `Graphics::Graphics()':
Graphics.h:27: error: cannot convert `char (*)[10]' to `char**' in assignment

Graphics.h: In destructor `Graphics::~Graphics()':
Graphics.h:35: error: cannot convert `char (*)[0u]' to `char**' in assignment

Graphics.h: In member function `void Graphics::SetWidth(int)':

Graphics.h:51: error: `width' cannot appear in a constant-expression
Graphics.h:69: error: `width' cannot appear in a constant-expression

Graphics.h: In member function `void Graphics::SetHeight(int)':
Graphics.h:88: error: `Graphics::Width' cannot appear in a constant-expression

Graphics.h:106: error: `Graphics::Width' cannot appear in a constant-expression

c:\dev-cpp\bin\make.exe: *** [main.o] Error 1

Execution terminated

BTW this is DevC++ Compiler

You have to allocate for each * in turn, starting with the outermost one, and working your way inwards.

Graphics::Graphics()
{
    Width = 10;
    Height = 10;
    TextArray = new char*[Height];
    for ( int h = 0 ; h < Height ; h++ ) {
        TextArray[h] = new char[Width];
    }
    ClearTextArray();
}
Graphics::~Graphics()
{
    for ( int h = 0 ; h < Height ; h++ ) {
        delete [] TextArray[h];
    }
    delete [] TextArray;
    TextArray = NULL;
    Width = 0;
    Height = 0;
}

Note the dtor does the same thing in reverse.

commented: Solved! tyvm +1

You have to allocate for each * in turn, starting with the outermost one, and working your way inwards.

Graphics::Graphics()
{
    Width = 10;
    Height = 10;
    TextArray = new char*[Height];
    for ( int h = 0 ; h < Height ; h++ ) {
        TextArray[h] = new char[Width];
    }
    ClearTextArray();
}
Graphics::~Graphics()
{
    for ( int h = 0 ; h < Height ; h++ ) {
        delete [] TextArray[h];
    }
    delete [] TextArray;
    TextArray = NULL;
    Width = 0;
    Height = 0;
}

Note the dtor does the same thing in reverse.

Ty man i'll try that and get back to you.


---------------------

Ty salem you solved it

Anyone who wants to use it here is it in the end:

class Graphics
{
public:
    Graphics();
    ~Graphics();
    
    bool PlotPixel(char,int,int);
    bool PlotSpirit(char**,int,int,int,int);
    void ClearTextArray();
    char** GetTextArray() { return TextArray; }
    int GetWidth() { return Width; }
    int GetHeight() { return Height; }
    void SetWidth(int);
    void SetHeight(int);
private:    
    char **TextArray;
    int Width;
    int Height;
};
Graphics::Graphics()
{
    Width = 10;
    Height = 10;
    TextArray = new char*[Height];
    for(int i = 0; i < Height; i++)
        TextArray[i] = new char[Width];
    ClearTextArray();
}
Graphics::~Graphics()
{
    for(int i = 0; i < Height; i++)
        delete [] TextArray[i];
    delete [] TextArray;
    TextArray = NULL;
    Width = 0;
    Height = 0;
}
void Graphics::SetWidth(int width)
{
    int dif = width - Width;
    if(dif < 0) // getting smaller
    {
        char buf[Height][width];
        int y = 0;
        int x = 0;
        for(y = 0; y < Height; y++)
            for(x = 0; x < width; x++)
                buf[y][x] = TextArray[y][x];

        for(y = 0; y < Height; y++)
            delete [] TextArray[y];
        delete [] TextArray;
        
        TextArray = new char*[Height];
        for(y = 0; y < Height; y++)
            TextArray[y] = new char[width];

        for(y = 0; y < Height; y++)
            for(x = 0; x < width; x++)
                TextArray[y][x] = buf[y][x];
        Width = width;
    }
    else // getting bigger
    {
        char buf[Height][width];
        int y = 0;
        int x = 0;
        for(y = 0; y < Height; y++)
            for(x = 0; x < Width; x++)
                buf[y][x] = TextArray[y][x];
        for(y = 0; y < Height; y++)
            for(x = x; x < width; x++)
                buf[y][x] = 0;
            
        for(y = 0; y < Height; y++)
            delete [] TextArray[y];
        delete [] TextArray;
        
        TextArray = new char*[Height];
        for(y = 0; y < Height; y++)
            TextArray[y] = new char[width];
        
        for(y = 0; y < Height; y++)
            for(x = 0; x < width; x++)
                TextArray[y][x] = buf[y][x];
        Width = width;
    }
}
void Graphics::SetHeight(int height)
{
    int dif = height - Height;
    if(dif < 0) // getting smaller
    {
        char buf[height][Width];
        int y = 0;
        int x = 0;
        for(y = 0; y < height; y++)
            for(x = 0; x < Width; x++)
                buf[y][x] = TextArray[y][x];
        
        for(y = 0; y < height; y++)
            delete [] TextArray[y];
        delete [] TextArray;
        
        TextArray = new char*[height];
        for(y = 0; y < height; y++)
            TextArray[y] = new char[Width];
        
        for(y = 0; y < height; y++)
            for(x = 0; x < Width; x++)
                TextArray[y][x] = buf[y][x];
        Height = height;
    }
    else // getting bigger
    {
        char buf[height][Width];
        int y = 0;
        int x = 0;
        for(y = 0; y < Height; y++)
            for(x = 0; x < Width; x++)
                buf[y][x] = TextArray[y][x];
        for(y = 0; y < height; y++)
            for(x = x; x < Width; x++)
                buf[y][x] = 0;
                
        for(y = 0; y < height; y++)
            delete [] TextArray[y];
        delete [] TextArray;
        
        TextArray = new char*[height];
        for(y = 0; y < Height; y++)
            TextArray[y] = new char[Width];
        
        for(y = 0; y < height; y++)
            for(x = 0; x < Width; x++)
                TextArray[y][x] = buf[y][x];
        Height = height;
    }
}
void Graphics::ClearTextArray()
{
    for(int i = 0; i < Height; i++)
        for(int n = 0; n < Width; n++)
            TextArray[i][n] = '\0';
}
bool Graphics::PlotPixel(char ch,int x,int y)
{
    if(x <= Width && y <= Height)
        if(x >= 0 && y >= 0)
        {
            TextArray[y][x] = ch;
            return true;
        }
    return false;
}
bool Graphics::PlotSpirit(char** array,int StartX,int StartY,int EndX,int EndY)
{
    if(StartX <= Width && StartX >= 0)
        if(EndX <= Width && EndX >= 0)
            if(StartY <= Height && StartY >= 0)
                if(EndY <= Height && EndY >= 0)
                {
                    for(int y = StartY; y < EndY; y++)
                        for(int x = StartX; x < EndX; x++)
                            TextArray[y][x] = array[y][x];
                    return true;
                }
    return false;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.