4

Having a problem in making a dynamic box using these set of ASCII codes. I'm really lost on what to do. This dynamic box also has numbers inside them.

I already tried making loops inside loops to adjust the box's size.

int main(){
    char asciis[] = {'\xDA','\xB3','\xC3','\xC0','\x20','\xC4','\xC5','\xC1','\xBF','\xB4','\xD9', '\xC2'};
    int box size = (n*2)+1;
    char box[box size][box size]; //set the size of the box
    //set a in all coordinates
    /*for (int r = 0; r < n; r++){
        for (int c = 0; c < n; c++){
            box[r][c] = 'a';
        }
    }*/

    for (int r = 0; r < n; r++){
        for (int c = 0; c < n; c++){
            for (int boxrow = 0; boxrow < box size; boxrow++){
                for (int boxcol = 0; boxcol < box size; boxcol++){
                    //conditions
                }
            }
        }
        cout << endl;
    }
}

This is the output I'm trying to create: enter image description here

https://i.stack.imgur.com/5huqR.png

Don't mind those numbers, I was just mapping the array.

3Dave
  • 28,657
  • 18
  • 88
  • 151
Razna
  • 73
  • 5
  • I'd recommend using [ncurses](https://www.gnu.org/software/ncurses/) (or one of its clones/derivatives) for something like this. – Jesper Juhl Sep 04 '19 at 14:31
  • 1
    Except for `\x20` = space, those aren't ASCII codes; they're [IBM437](https://en.wikipedia.org/wiki/Code_page_437) codes. I assume your program is running on a DOS or Windows system? – dan04 Sep 04 '19 at 15:25

1 Answers1

5

I'm sure there's a simpler solution, but off the top of my head:

#include <iostream>

using namespace std;

enum  BoxParts {
    kLeftUpper = '\xDA',
    kVertical = '\xB3',
    kLeftBreak = '\xC3',
    kLeftLower = '\xC0',
    kEmpty = '\x20',
    kHorizontal = '\xC4',
    kIntersection = '\xC5',
    kBottomBreak = '\xC1',
    kRightUpper = '\xBF',
    kRightBreak = '\xB4',
    kRightLower = '\xD9',
    kTopBreak = '\xC2',
    kCount = 12
};

void drawLine(const int cellCount, const int cellWidth, const char left, const char divider, const char contents, const char right)
{
    cout << left;
    for (int i = 1; i < cellCount * cellWidth; ++i)
    {
        if (0 == i % cellWidth)
            cout << divider;
        else
            cout << contents;
    }
    cout << right << endl;
}

void drawBox(const int cellCount, const int cellWidth, const int cellHeight)
{
    // top
    drawLine(cellCount, cellWidth, kLeftUpper, kTopBreak, kHorizontal, kRightUpper);
    for (int i = 1; i < cellCount * cellHeight; ++i)
    {
        if (0 == i % cellHeight)
            drawLine(cellCount, cellWidth, kLeftBreak, kIntersection, kHorizontal, kRightBreak);
        else
            drawLine(cellCount, cellWidth, kVertical, kVertical, ' ', kVertical);
    }
    // bottom
    drawLine(cellCount, cellWidth, kLeftLower, kBottomBreak, kHorizontal, kRightLower);
}



int main(int argc, char** argv) {

    const int n = 4;
    drawBox(n, 10, 5);
    getchar();

    return 0;
}

Produces:

enter image description here

3Dave
  • 28,657
  • 18
  • 88
  • 151