Search code examples
cinputterminalgetchar

Need help understanding how to use get char to type out sentences in terminal


#include <stdio.h>
#include <string.h>

int row;
int col;
int input;
int offset;
char ascii[7][580] = {
    "        *    * *    * *   *   **     **       *   *     *                                     *  ***  ***** ***** ***** ***** ***** ***** ***** ***** *****                *         *      **   ***   ***  ****   **** ****  ***** *****  **** *   * ***** ***** *   * *     *   * *   *  ***  ****   ***  ****   **** ***** *   * *   * *   * *   * *   * *****  ***  *      ***    *         *         *               *         **        *                        **                                              *                                          **    *    **        ",
    "        *    * *    * *  **** **  * *  *     *   *       *  * * *   *                        *  *   * ***** ***** ***** ***** ***** ***** ***** ***** *****   **    **    *           *       * *   * *   * *   * *     *   * *     *     *     *   *   *      *  *  *  *     ** ** **  * *   * *   * *   * *   * *       *   *   * *   * *   * *   * *   *     *  *     *       *   * *         *        *               *        *  *       *                         *                                              *                                         *      *      *       ",
    "        *    * *  ***** * *      *  * *     *   *         *  ***    *                        *  *   * ***** ***** ***** ***** ***** ***** ***** ***** *****   **    **   *    *****    *      * *  ** *   * *   * *     *   * *     *     *     *   *   *      *  * *   *     ** ** **  * *   * *   * *   * *   * *       *   *   * *   * *   *  * *   * *     *   *     *       *  *   *         *  ***  *      ***      *  ***   *     **** *       *     *   *  *    *   ****  ****   ***  ****   **** * **   ***  ***** *   * *   * *   * *   * *   * *****  *      *      *   *   ",
    "        *          * *   ***    *    *          *         *   *    ***        *****         *   *   * ***** ***** ***** ***** ***** ***** ***** ***** *****             *               *   **  * * * ***** ****  *     *   * ***** ***** * *** *****   *      *  **    *     * * * * * * *   * ****  *   * ****   ***    *   *   *  * *  * * *   *     *     *    *      *      *                      * ****  *   *  **** *   * ***** *   * ****              * *     *   * * * *   * *   * *   * *   * *   * *      *    *   * *   * *   *  * *   * *     *  *       *       * * * *",
    "        *         *****   * *  *    * * *       *         *  ***    *     **          **   *    *   * ***** ***** ***** ***** ***** ***** ***** ***** *****   **    **   *    *****    *    *   *  *  *   * *   * *     *   * *     *     *   * *   *   *      *  * *   *     *   * *  ** *   * *     * * * *   *     *   *   *   *  * *  ** **  * *    *    *     *       *     *                   **** *   * *     *   * *****  *     **** *   *   *     *   **      *   * * * *   * *   * ****   **** *      ***   *    *   *  * *  * * *   *     *     *    *      *      *     * ",        
    "                  * *   ****  *  ** *  *         *       *  * * *   *     **          **   *    *   * ***** ***** ***** ***** ***** ***** ***** ***** *****   **    **    *           *         *     *   * *   * *     *   * *     *     *   * *   *   *   *  *  *  *  *     *   * *  ** *   * *     *  ** *   *     *   *   *   *  * *  ** ** *   *   *   *      *       *     *                  *   * *   * *   * *   * *      *        * *   *   *     *   **      *   * * * *   * *   * *         * *         *  *  * *   *  * *  * * *  * *   *     *     *      *      *       ",
    "        *         * *     *      **  ** *         *     *                 *               *      ***  ***** ***** ***** ***** ***** ***** ***** ***** *****         *      *         *      *    ***  *   * ****   **** ****  ***** *      ***  *   * *****  **   *   * ***** *   * *   *  ***  *      **** *   * ****    *    ***    *   *   * *   *   *   *****  ***      *  ***        *****      ***   ***   ***   ***   ***   *     ***  *   *  ***  **    *  *   ***  * * * *   *  ***  *       **  *      ***    **   ****   *    * *  * * * *     *****   **    *    **        ",

};


int main(int argc, char const *argv[]){



//for loop for the rows down 
for(row = 0; row < 7; row++){
    printf("%c", ascii[row][col]);
        //columns for each segment of stars or space
        for(col = 0; col < 6; col++){

        }//end of cols
    }//end of rows
}

I have used a 2D array to store a mock up of the ascii values, I haven't added numbers I would like to be able to use terminal to input letters and symbols then for them to print in star value can anyone help me understand how to do this.


Solution

  • Assuming the commented out for loop assigns the current character to the variable ch:

    putchar(ascii[row][(ch - 32)*6 + col]);
    

    (ch-32) will take you to the beginning of the "font".

    (ch-32)*6 will skip all "star columns" of all previous characters in the font.

    (ch-32)*7 + col selects the current "star column" of the current character.

    void print_line(char *str)
    {
        char ch, *p;
        int row, col;
        for (row = 0; row < 7; row++)
        {
            for (p = str; ch = *p; p++)
            {
                /* Check that ch is in the printable range of ASCII. */
                if (ch < 32 || ch > 95)
                    ch = '?';  /* or continue; */
                for (col = 0; col < 6; col++)
                    putchar(ascii[row][(ch - 32)*6 + col]);
            }
            putchar('\n');
        }
    }
    

    This function can only handle one line at a time.