0

The following program is a work in progress and is designed to allow someone to hang simulated pictures on a simulated wall in a very, very primitive graphical environment.

I compiled the program on Windows with Dev-C++ and it runs just as intended. I compiled it on Mac OSX with Clang and it does not. On OSX, the 2D character array does not print properly. The right-most wall side ends up being printed in the middle of the wall, and when the program prints the "pictures," the pictures do not print properly at all. They throw off the spacing for that right-most wall-side (resulting in certain asterisks on that wall-side appearing jagged and out of the appropriate column).

Is this a function of the two different compilers? I don't see how it could be unless maybe there is undefined behavior going on in my code?

Or is it a function of the two different command line environments? I.e., the character output works as intended with the Windows command prompt but not with Mac's terminal?

Also, please disregard my use of the round() function from the math.h header in the beginning; initially I wanted to make sure the rounding was correct but I quickly realized that since I'm only ever dividing integers by 2, whether the program rounds up or down doesn't make much of a difference (it's .5 in either direction). That's why the function appears in the beginning of the program but not at the end. I'm going to either take it out completely or use it for all of the division in the program.

Code:

#include <stdio.h>
#include <math.h>

int main(void)
{
    char wall[1000][1000];
    int rows;
    int columns;
    int wfeet;
    int winches;
    int hfeet;
    int hinches;
    int wtotalinches;
    int htotalinches;
    int xcoordscols;
    int ycoordsrows;
    int rowunderscorechars;
    int columnpipechars;
    int i;
    int j;
    int k;
    int p;
    int xcoordTL[500];
    int ycoordTL[500];
    int xcoordTR[500];
    int ycoordTR[500];
    int xcoordBL[500];
    int ycoordBL[500];
    int xcoordBR[500];
    int ycoordBR[500];
    int picwidth[500];
    int picheight[500];

    p = 0;

    printf("How wide is the wall? You'll first enter feet only. Then you'll need to enter inches (if any).\n");
    printf("Please enter the width of the wall in feet (don't worry about inches yet):\n");
    scanf("%d", &wfeet);
    printf("Please enter any extra inches now.\n");
    scanf("%d", &winches);
    printf("Great! Now how TALL is the wall? Again, feet first, inches second.\n");
    printf("Please enter the height of the wall in feet (don't worry about inches yet):\n");
    scanf("%d", &hfeet);
    printf("Please enter any extra inches now.\n");
    scanf("%d", &hinches);

    wtotalinches = (wfeet * 12) + winches;
    htotalinches = (hfeet * 12) + hinches;

    ycoordsrows = round((float)htotalinches / 2);
    xcoordscols = round((float)wtotalinches / 2);

    for (i = 0; i < xcoordscols; i++)
    {
        wall[0][i] = '*';

    }
    for (i = 0; i < xcoordscols; i++)
    {
        wall[ycoordsrows - 1][i] = '*';
    }

    for (i = 0; i < ycoordsrows; i++)
    {
        wall[i][0] = '*';
    }

    for (i = 0; i < ycoordsrows; i++)
    {
        wall[i][xcoordscols - 1] = '*';
    }

    for (k = 0; k < ycoordsrows; k++)
    {
        for (j = 0; j < xcoordscols; j++)
        {
            printf("%c", wall[k][j]);
            printf(" ");
        }
    printf("\n");
    }

    while (1)
    {
        printf("Great! Now I need the x and y coordinates for the UPPER LEFT hand corner of the picture or object to be hung.\n");
        printf("So pick out where you want the picture or object hung, then figure out where the upper left hand corner of that picture or object ");
        printf("would be on the wall. Then, count the *s to get the x and y coordinates for the UPPER LEFT hand corner of the picture or object.\n");
        printf("First, the x coordinate (of the upper left hand corner of the picture or object):\n");
        scanf("%d", &xcoordTL[p]);
        printf("Great! Now, the y coordinate (counting DOWN from the top):\n");
        scanf("%d", &ycoordTL[p]);
        printf("Great! Now, enter the dimensions of the picture, in INCHES only.\n");
        printf("First, the width of the picture or object (in inches):\n");
        scanf("%d", &picwidth[p]);
        printf("Now the height of the picture or object (in inches):\n");
        scanf("%d", &picheight[p]);

        xcoordTR[p] = xcoordTL[p] + ((picwidth[p] / 2) - 1);
        ycoordTR[p] = ycoordTL[p];

        xcoordBL[p] = xcoordTL[p];
        ycoordBL[p] = ycoordTL[p] + ((picheight[p] / 2) - 1);

        xcoordBR[p] = xcoordTR[p];
        ycoordBR[p] = ycoordBL[p];

        for (i = xcoordTL[p] - 1; i <= xcoordTR[p] - 1; i++)
        {
            wall[ycoordTL[p] - 1][i] = '*';
        }

        for (i = xcoordBL[p] - 1; i <= xcoordBR[p] - 1; i++)
        {
            wall[ycoordBL[p] -1][i] = '*';
        }

        for (i = ycoordTL[p] - 1; i <= ycoordBL[p] - 1; i++)
        {
            wall[i][xcoordTL[p] - 1] = '*';
        }

        for (i = ycoordTR[p] - 1; i <= ycoordBR[p] - 1; i++)
        {
            wall[i][xcoordTR[p] - 1] = '*';
        }

        for (k = 0; k < ycoordsrows; k++)
        {
            for (j = 0; j < xcoordscols; j++)
            {
                printf("%c", wall[k][j]);
                printf(" ");
            }
        printf("\n");
        }
    p++;
    }
    return 0;
}
Andronicus
  • 25,419
  • 17
  • 47
  • 88
Learning
  • 35
  • 6
  • Can you show the output on both systems? – user1118321 Aug 14 '19 at 05:13
  • Here's something to think about - What's in the `wall` array to start with? You fill out the sides after the user enters the width and height of the wall, but what's in between the sides? – user1118321 Aug 14 '19 at 05:26
  • 1
    OSX Terminal wraps at 80 characters, which is likely the problem. `printf` isn't designed to do terminal UI. For that you need a TUI framework, like [ncurses](https://en.wikipedia.org/wiki/Ncurses) or similar – nickelpro Aug 14 '19 at 05:31
  • Allocating this many big arrays on the stack is a bad idea. See [Getting a stack overflow exception when declaring a large array](https://stackoverflow.com/questions/571945/getting-a-stack-overflow-exception-when-declaring-a-large-array). – Lundin Aug 14 '19 at 06:24

0 Answers0