Why does the first column not get printed when I am using gotoxy(x,y)

Viewed 47

So I am building a map with borders and filling up with ' * '

Now what I want to do is empty all of the ' * ' and fill them up with blank spaces. I am not getting the expected output and can't figure out what I am doing wrong, I'd really appreciate if someone could help me.

#include <stdio.h>
#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))


int height=5;
int width=5;

void fill_blank_spaces()
{
    gotoxy(0,0);
    for(int i=0;i<height;i++)
    {
        for(int j=0;j<width;j++)
        {    if(i!=0 && j!=0 && i!=height-1 && j!=width-1)
                printf(" ");
        }
        printf("\n");
    } 
}

I expect the output to be:

X---X
|   |
|   |
|   |
X---X

But the displayed output is:

X---X
   *|
   *|
   *|
X---X
int main()
{
    for (int i = 0; i < height; i++)
    {
            for (int j = 0; j < width; j++)
            {
                if ((i == 0 && j == 0) || (i == 0 && j == width - 1) || (j == 0 && i == height - 1) || (j == width - 1 && i == height-1))
                     printf("X");
                else if ((j == 0) || (j == width - 1))
                    printf("|");
                else if (i == height - 1 || i == 0)
                    printf("-");
                else
                    printf("*") ;
            }
            printf("\n");
    }   
    fill_blank_spaces(); 
}

I am new here so excuse my unconventional description.

2 Answers

The top left corner is at ( 1, 1).
The first space on the following lines should go in
( 2, 2), ( 2, 3) and ( 2, 4)

#include <stdio.h>
#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))

int height=5;
int width=5;

void fill_blank_spaces()
{
    for ( int i = 1; i < height - 1; i++)
    {
        for ( int j = 1; j < width - 1; j++)
        {
            gotoxy ( j + 1, i + 1);
            printf ( " ");
        }
    }
    printf ( "\n");
    printf ( "\n");
}

int main ( void)
{
    gotoxy ( 1, 1);
    for ( int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if ((i == 0 && j == 0) || (i == 0 && j == width - 1) || (j == 0 && i == height - 1) || (j == width - 1 && i == height-1))
                 printf("X");
            else if ((j == 0) || (j == width - 1))
                printf("|");
            else if (i == height - 1 || i == 0)
                printf("-");
            else
                printf("*") ;
        }
        printf("\n");
    }
    fill_blank_spaces();
}

You don't need fill_blanck_spaces, simply replace * by a space :

int main()
{
    for (int i = 0; i < height; i++)
    {
            for (int j = 0; j < width; j++)
            {
                if ((i == 0 && j == 0) || (i == 0 && j == width - 1) || (j == 0 && i == height - 1) || (j == width - 1 && i == height-1))
                     printf("X");
                else if ((j == 0) || (j == width - 1))
                    printf("|");
                else if (i == height - 1 || i == 0)
                    printf("-");
                else
                    printf(" ") ; //<=== Here
            }
            printf("\n");
    }
    return 0;   
}
Related