Thread: Text to BLOCK text (Function)

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    217

    Thumbs up Text to BLOCK text (Function)

    Hey guys, this is going to be an all in one thread. This is the first ever function I have ever created and it's a really big deal for me so I'm really excited to introduce it to y'all.

    Okay so now about the all in one part. I'm posting this here to get suggestions on improving the function (above anything). I'm also posting this here to hopefully share it's use with others. I personally wrote this function to use it in my console applications.

    I wrote every single letter in my function and it must have taken at least 2 hours.

    I'm sharing this because I could not find anything similar on the internet and I'm hoping that one day this will be of use to somebody.

    So feel free to use it, you don't have to credit me.


    Compatibility Notes:
    ...

    Pre-requisite:
    Add this to the beginning of your code, windows.h is for set_position function and set_position is used for setting the console cursor position which is important for the function.

    Code:
    #include<Windows.h>
    
    void set_position(int columns, int rows) {
        HANDLE hStdout;
        COORD destCoord;
        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    
        destCoord.X = columns;
        destCoord.Y = rows;
    
        SetConsoleCursorPosition(hStdout, destCoord);
    } // Changes the position of the console cursor


    instructions:


    0) You don't have to credit me for anything. You can edit and use the function as you like it!

    1) Make sure you include everything in the class to your script and make sure your compiler reads it before calling it.

    2) There are support for A-Z in capitals, !, ?, ~ (types a block)

    3) The dimensions of a letter is 6columns x 5rows.

    Using the function:

    To use the function, make an object and call .type() using the object.

    ex:
    Code:
    block_text text_pointer;
    text_pointer.type("!");
    Parameters for the function:
    (text_to_type, columns, rows)

    text_to_type: Must be a const char variable or a string constant (ex: "HELLO")

    --> You must use CAPITAL LETTERS while writing the letters, this is important.

    columns: This instructs the function from where to begin the type in terms of columns.

    rows: This instructs the function from where to begin the type in terms of rows.


    How to use the function text parameter:


    1) \t and " " will cause the function to leave 1 column. You can make use of this and type multiple whitespaces to get the required amount of space between the characters.

    2) \n will bring the cursor right below the previos text but starting from the column which the type had been given in the parameter.

    3) Default parameters for "rows" and "columns" and set to 1 and 1 respectively, and for text_to_type, default is "" (empty);

    4) The function automatically types text besides each other, you don't have to worry about setting the position of the cursor.

    5) When the parameter for text_to_type is empty, the function can be used to set the console cursor position. (However it's better to use the set_position function directly itself.


    Examples:
    will be edited in shortly.



    I would love to get any feedback, problems, and definitely pointers.
    Cheers!



    (The function makes use of set_position, class, (char)219 ASCII value and two variables determining the column and row.

    Function source code:
    Code:
    class block_text {
    
    public:
        void type(const char text_to_type[] = "", int columns = 1, int rows = 1) {
    
            column = columns;
            row = rows;
    
            original_column = column;
            original_row = column;
    
            set_position(column, row);
    
            for (int i = 0; i < strlen(text_to_type); i++) {
                switch (text_to_type[i]) {
                case '\n':
                    type_newline();
                    break;
                case '\t':
                case ' ':
                    type_space();
                    break;
                case 'A':
                    type_A();
                    break;
                case 'B':
                    type_B();
                    break;
                case 'C':
                    type_C();
                    break;
                case 'D':
                    type_D();
                    break;
                case 'E':
                    type_E();
                    break;
                case 'F':
                    type_F();
                    break;
                case 'G':
                    type_G();
                    break;
                case 'H':
                    type_H();
                    break;
                case 'I':
                    type_I();
                    break;
                case 'J':
                    type_J();
                    break;
                case 'K':
                    type_K();
                    break;
                case 'L':
                    type_L();
                    break;
                case 'M':
                    type_M();
                    break;
                case 'N':
                    type_N();
                    break;
                case 'O':
                    type_O();
                    break;
                case 'P':
                    type_P();
                    break;
                case 'Q':
                    type_Q();
                    break;
                case 'R':
                    type_R();
                    break;
                case 'S':
                    type_S();
                    break;
                case 'T':
                    type_T();
                    break;
                case 'U':
                    type_U();
                    break;
                case 'V':
                    type_V();
                    break;
                case 'W':
                    type_W();
                    break;
                case 'X':
                    type_X();
                    break;
                case 'Y':
                    type_Y();
                    break;
                case 'Z':
                    type_Z();
                    break;
                case '!':
                    type_excl();
                case '?':
                    type_ques();
                case '~':
                    type_bloc();
                    
    
    
                default:
                    break;
                }
    
            }
    
    
        }
    
    private:
    
        int column = 1, original_column = 1;
        int row = 1, original_row = 1;
    
    
        void type_newline() {
            row = row + 6;
            column = original_row;
        }
    
        void type_space() {
            column = column + 1;
        }
    
        void type_A() {
            set_position(column, row);
            std::cout << " " << (char)219 << (char)219 << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
    
            column = column + 7;
        }
    
        void type_B() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << std::endl;
    
            column = column + 7;
        }
    
        void type_C() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
    
            column = column + 7;
        }
    
        void type_D() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << std::endl;
    
            column = column + 7;
        }
    
        void type_E() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219;
    
            column = column + 7;
        }
    
        void type_F() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 << (char)219 << (char)219 << (char)219;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219;
    
            column = column + 7;
        }
    
        void type_G() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219; 
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219;
    
            column = column + 7;
        }
    
        void type_H() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
    
            column = column + 7;
        }
    
        void type_I() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;;
            set_position(column, row + 1);
            std::cout << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
    
            column = column + 7;
        }
    
        void type_J() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;;
            set_position(column, row + 1);
            std::cout << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 << (char)219 << (char)219 << std::endl;
            column = column + 7;
        }
    
        void type_K() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << " " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 << (char)219 << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << " " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            column = column + 7;
        }
    
        void type_L() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
    
            column = column + 7;
        }
    
        void type_M() {
            set_position(column, row);
            std::cout << (char)219 << "    " << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
    
            column = column + 7;
        }
    
        void type_N() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 <<  (char)219 << " " << (char)219 << (char)219 <<  std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << " "  << (char)219 << (char)219 << (char)219 <<  std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
    
            column = column + 7;
        }
    
        void type_O() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
    
            column = column + 7;
        }
    
        void type_P() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 << std::endl;
    
            column = column + 7;
        }
    
        void type_Q() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << " " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << "   " << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << " " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
    
            column = column + 7;
        }
    
        void type_R() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << " "  << (char)219 << (char)219 << (char)219 <<  std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 << (char)219 << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << " " << (char)219 << (char)219  << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
    
            column = column + 7;
        }
    
        void type_S() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row+1);
            std::cout << (char)219 << (char)219 << "    " << std::endl;
            set_position(column, row+2);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row+3);
            std::cout << "    " << (char)219 << (char)219 << std::endl;
            set_position(column, row+4);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
    
            column = column + 7;
        }
    
        void type_T() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 1);
            std::cout << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << "  " << (char)219 << (char)219 << std::endl;
    
            column = column + 7;
        }
    
        void type_U() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << " " << (char)219 << (char)219  << (char)219 << (char)219 << " " << std::endl;
    
            column = column + 7;
        }
    
        void type_V() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << " " << (char)219 << "  " << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << " " << (char)219 << "  " << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << " " << (char)219 << "  " << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << "  " << (char)219 << (char)219 << "  " << std::endl;
    
            column = column + 7;
        }
    
        void type_W() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << "    " << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << " " << (char)219  << (char)219 << " " << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << " " << (char)219  << (char)219 << " " << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
    
            column = column + 7;
        }
    
        void type_X() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << "  " << (char)219 << (char)219 << "  " << std::endl;
            set_position(column, row + 2);
            std::cout << " " << (char)219 << (char)219  << (char)219 << (char)219 << " " << std::endl;
            set_position(column, row + 3);
            std::cout << "  " << (char)219 << (char)219 << "  " << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
    
            column = column + 7;
        }
    
        void type_Y() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << " " << (char)219 << "  " << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << " " << (char)219 << (char)219 << (char)219 << (char)219 << std::endl;
            set_position(column, row + 3);
            std::cout << "   " << (char)219  << std::endl;
            set_position(column, row + 4);
            std::cout << "  " << (char)219 << (char)219 << "  " << std::endl;
    
            column = column + 7;
        }
    
        void type_Z() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 1);
            std::cout << "   " << (char)219 << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << "  " << (char)219 << (char)219 << "  " << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
    
            column = column + 7;
        }
    
        void type_excl() {
            set_position(column, row);
            std::cout << "  " << (char)219 << (char)219  << std::endl;
            set_position(column, row + 1);
            std::cout << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << "  " << (char)219 << (char)219 << std::endl;
    
            column = column + 7;
        }
    
        void type_ques() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << "    " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << " " << (char)219 << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << " " << (char)219 << (char)219 << "  " << std::endl;
    
            column = column + 7;
        }
    
        void type_bloc() {
            set_position(column, row);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
    
            column = column + 7;
        }
    Last edited by Nwb; 09-22-2018 at 10:23 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There are several programs which do this. The original of which (for those of a 'certain age') is called banner.
    How can I make ascii-banners from the command line? - Ask Ubuntu

    My first suggestion would be to replace 400+ lines of code with something more general.

    Consider reading a file such as this into some kind of data structure.
    Code:
    Size=6x5
    Letter=A
    ..X...
    .X.X..
    X...X.
    XXXXX.
    X...X.
    Letter=B
    XXX...
    X..X..
    XXX...
    X..X..
    XXX...
    The actual code to display any letter reduces to
    Code:
    for ( r = 0 ; r < nRows ; r++ ) {
        set_position(column, row + r);
        for ( c = 0 ; c < nCols ; c++ ) {
            if ( map[r][c] == 'X' ) cout << (char)219;
            else cout << ' ';
        }
    }
    where 'map' is the 2D lookup table array you read from the file corresponding to the letter you want to display.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    From what I see, you basically have a single function in the public interface that takes a string, and for each character in the string, maps that character to a function that is part of the private implementation. If such a mapping exists, it then calls that corresponding function.

    Consequently, it looks like you don't actually need a class: one reason why you just have a class because you wanted to make those implementation functions private, but you don't need a class for that. Rather, you could have used an unnamed namespace. Another reason is because you have state, but your state doesn't seem to be maintained between calls to the type member function, but rather is only maintained across the duration of iterating over the string. Therefore, in the implementation file you could have gone for something like:
    Code:
    #include <iostream>
    #include <cstring>
    #include <windows.h>
    
    namespace
    {
        // Changes the position of the console cursor
        void set_position(int columns, int rows) {
            HANDLE hStdout;
            COORD destCoord;
            hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    
            destCoord.X = columns;
            destCoord.Y = rows;
    
            SetConsoleCursorPosition(hStdout, destCoord);
        }
    
        // ...
    
        int type_A(int& column, int& row) {
            set_position(column, row);
            std::cout << " " << (char)219 << (char)219 << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
    
            column = column + 7;
        }
    
        // ...
    }
    
    namespace block_text
    {
        void type(const char text_to_type[], int column, int row) {
            using namespace std;
    
            set_position(column, row);
    
            size_t len = strlen(text_to_type);
            for (size_t i = 0; i < len; ++i) {
                switch (text_to_type[i]) {
                    // ...
                case 'A':
                    type_A(column, row);
                    break;
            // ...
        }
    }
    Then in the header:
    Code:
    #ifndef BLOCK_TEXT_H_
    #define BLOCK_TEXT_H_
    
    namespace block_text
    {
        void type(const char text_to_type[], int column = 1, int row = 1);
    }
    
    #endif
    You would no longer need to specify your "pre-requisite", except to note that <windows.h> is a pre-requisite (but perhaps that should go into your empty "Compatibility Notes").

    Also, instead of doing stuff like:
    Code:
    std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
    Just write a helper function (in the unnamed namespace):
    Code:
    void print_char219(int n)
    {
        for (int i = 0; i < n; ++i) {
            std::cout << (char)219;
        }
    }
    
    // ...
    print_char219(6);
    std::cout << std::endl;
    If you want to be fancy and have an output manipulator:
    Code:
    class print_char219 {
    public:
        explicit print_char219(int n) : n(n) {}
    
        std::ostream& print(std::ostream& out) const {
            for (int i = 0; i < n; ++i) {
                out << (char)219;
            }
            return out;
        }
    private:
        int n;
    };
    
    std::ostream& operator<<(std::ostream& out, const print_char219& obj) {
        obj.print(out);
        return out;
    }
    
    // ...
    std::cout << print_char219(6) << std::endl;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    Quote Originally Posted by Salem View Post
    There are several programs which do this. The original of which (for those of a 'certain age') is called banner.
    How can I make ascii-banners from the command line? - Ask Ubuntu

    My first suggestion would be to replace 400+ lines of code with something more general.

    Consider reading a file such as this into some kind of data structure.
    Code:
    Size=6x5
    Letter=A
    ..X...
    .X.X..
    X...X.
    XXXXX.
    X...X.
    Letter=B
    XXX...
    X..X..
    XXX...
    X..X..
    XXX...
    The actual code to display any letter reduces to
    Code:
    for ( r = 0 ; r < nRows ; r++ ) {
        set_position(column, row + r);
        for ( c = 0 ; c < nCols ; c++ ) {
            if ( map[r][c] == 'X' ) cout << (char)219;
            else cout << ' ';
        }
    }
    where 'map' is the 2D lookup table array you read from the file corresponding to the letter you want to display.
    That's cool I never knew about it. How do I go about implementing this terminal command in my console program (I'm really a newbie at this..) and is it supported in windows?
    By the way thanks a lot for the suggestion I appreciate it!

  5. #5
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    Quote Originally Posted by laserlight View Post
    From what I see, you basically have a single function in the public interface that takes a string, and for each character in the string, maps that character to a function that is part of the private implementation. If such a mapping exists, it then calls that corresponding function.

    Consequently, it looks like you don't actually need a class: one reason why you just have a class because you wanted to make those implementation functions private, but you don't need a class for that. Rather, you could have used an unnamed namespace. Another reason is because you have state, but your state doesn't seem to be maintained between calls to the type member function, but rather is only maintained across the duration of iterating over the string. Therefore, in the implementation file you could have gone for something like:
    Code:
    #include <iostream>
    #include <cstring>
    #include <windows.h>
    
    namespace
    {
        // Changes the position of the console cursor
        void set_position(int columns, int rows) {
            HANDLE hStdout;
            COORD destCoord;
            hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    
            destCoord.X = columns;
            destCoord.Y = rows;
    
            SetConsoleCursorPosition(hStdout, destCoord);
        }
    
        // ...
    
        int type_A(int& column, int& row) {
            set_position(column, row);
            std::cout << " " << (char)219 << (char)219 << (char)219 << (char)219 << std::endl;
            set_position(column, row + 1);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 2);
            std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
            set_position(column, row + 3);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
            set_position(column, row + 4);
            std::cout << (char)219 << (char)219 << "  " << (char)219 << (char)219 << std::endl;
    
            column = column + 7;
        }
    
        // ...
    }
    
    namespace block_text
    {
        void type(const char text_to_type[], int column, int row) {
            using namespace std;
    
            set_position(column, row);
    
            size_t len = strlen(text_to_type);
            for (size_t i = 0; i < len; ++i) {
                switch (text_to_type[i]) {
                    // ...
                case 'A':
                    type_A(column, row);
                    break;
            // ...
        }
    }
    Then in the header:
    Code:
    #ifndef BLOCK_TEXT_H_
    #define BLOCK_TEXT_H_
    
    namespace block_text
    {
        void type(const char text_to_type[], int column = 1, int row = 1);
    }
    
    #endif
    You would no longer need to specify your "pre-requisite", except to note that <windows.h> is a pre-requisite (but perhaps that should go into your empty "Compatibility Notes").

    Also, instead of doing stuff like:
    Code:
    std::cout << (char)219 << (char)219 <<  (char)219 << (char)219 << (char)219 << (char)219  << std::endl;
    Just write a helper function (in the unnamed namespace):
    Code:
    void print_char219(int n)
    {
        for (int i = 0; i < n; ++i) {
            std::cout << (char)219;
        }
    }
    
    // ...
    print_char219(6);
    std::cout << std::endl;
    If you want to be fancy and have an output manipulator:
    Code:
    class print_char219 {
    public:
        explicit print_char219(int n) : n(n) {}
    
        std::ostream& print(std::ostream& out) const {
            for (int i = 0; i < n; ++i) {
                out << (char)219;
            }
            return out;
        }
    private:
        int n;
    };
    
    std::ostream& operator<<(std::ostream& out, const print_char219& obj) {
        obj.print(out);
        return out;
    }
    
    // ...
    std::cout << print_char219(6) << std::endl;
    Dude thanks a lot that's really helpful!

  6. #6
    Registered User
    Join Date
    Sep 2018
    Posts
    217
    Quote Originally Posted by Nwb View Post
    That's cool I never knew about it. How do I go about implementing this terminal command in my console program (I'm really a newbie at this..) and is it supported in windows?
    By the way thanks a lot for the suggestion I appreciate it!
    ^^^^^^^^^

    [spoiler]testing [/spoiler]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User enter text and save to text file (end enter text using -1)
    By DecoratorFawn82 in forum C Programming
    Replies: 7
    Last Post: 12-28-2017, 04:23 PM
  2. Making a string from a text file after a specific text
    By GypsyV3nom in forum C Programming
    Replies: 4
    Last Post: 06-22-2016, 01:11 PM
  3. Replies: 10
    Last Post: 10-14-2010, 09:35 AM
  4. Type text = Press button = Display text in Google?
    By Raze88 in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2008, 08:39 AM
  5. Replies: 4
    Last Post: 03-18-2008, 09:03 PM

Tags for this Thread