3
#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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dominic Baker
  • 51
  • 1
  • 10
  • Please format the code properly. – Oskar Skog Oct 21 '16 at 13:22
  • `ascii[7][580]` is [UB](http://stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviours-that-a-c-programmer-should-know-a) as you are reading beyond the boundary of the array. I think you want `ascii[row][col]`. – 001 Oct 21 '16 at 13:23
  • Adding examples of what you've tried woud help us understand where you are getting stuck. – Vaughn Cato Oct 21 '16 at 13:25
  • @OskarSkog what is wrong with the format? and I know its reading beyond at the moment but I'm trying to work out how to use getchar to call elements within the array – Dominic Baker Oct 21 '16 at 13:26
  • I'm using a loop within a loop within a loop to access each character that I have stored i just don't know how to call each character and assign them to each character input – Dominic Baker Oct 21 '16 at 13:29
  • It's generally recommended to indent the function bodies. You have a } outside the code block. The commented out for loop misses a commented out }. The second to last } is indented too far. – Oskar Skog Oct 21 '16 at 13:32
  • Why are you printing `ascii[7][580]` over and over ? BTW indexes `[7][580]` are out of bounds. – Jabberwocky Oct 21 '16 at 13:36
  • You should have a 3D array instead of a @D array. That way, you can access each character individually. – dbush Oct 21 '16 at 13:47
  • the `col` variable is being used in the `printf()` statement before it is set. For this and other reasons, the posted code does not compile. – user3629249 Oct 22 '16 at 07:08

3 Answers3

2

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.

Oskar Skog
  • 332
  • 5
  • 16
2

You almost certainly want to structure your data differently.

It looks like each character is defined in as 7 rows of 6 characters (one of which is always a space - i.e. an empty column).

Consider instead a structure something like this:

typedef struct big_char { 
    unsigned char the_char;
    char *representation[7];
} big_char;

Now, you can represent a single character thus:

big_char asterisk = { '*' , {
    "     ",
    "* * *",
    " * * ",
    "  *  ",
    " * * ",
    "* * *",
    "     "
}};

Which you could print with

for (row = 0; row < 7; ++row) {
    printf ("%s\n", asterisk.representation[row]);
}

You can see how this forms the basis for a simple function to print a large character.

Your ascii table is then an array of these structures.

When the user enters a character, you search the array looking for a match with ascii[i].the_char and then print out the entry.

kdopen
  • 8,032
  • 7
  • 44
  • 52
1

If you want to do it with something similar to your struct, you can also just calculate an offset. The following code example calculates the offset for you. Each Ascii symbol has a corresponding integer value between 0 and 127 with [space] being 32. Thus your offset is 6 columns times integer of desired ascii symbol - 32 (as [space] should not have an offset).

Edit: I tested it with the correct ascii numbers and now it works

// variable to read in letter

char input = 0;
int offset = 0;

// read in of letter

printf("Please enter the letter you want: \n");
scanf("%c", &input);

// get offset from letter. [space] is ascii symbol nb. 32


offset = 6*( (int) input - 32);

// print letter

for(int row = 0; row < 7; row++){

    for(int col = 0; col < 6; col++){

        printf("%c", ascii[row][offset+col]);

    }

    printf("\n");
}

return 0;
maow
  • 2,712
  • 1
  • 11
  • 25