Program ending unexpectedly

Viewed 34

Why is this program running fine till a is 5. [as soon as a becomes 6, the program prints its last value (i.e., '000007') and ends without even printing the 'Program END' msg, in the last and without even executing the last getch();] While this program is supposed to work like: print a-1 number of 0 and print 7 at last; this process should repeat itself every time the user enters any character, with an increment in the value of a. I want all of this to happen at the center, an wanted the '7' to be printed seperately, so I used created gotoxy function, and printed the 7 seperately.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

void gotoxy(int x, int y)
{
    COORD c;
    c.X = x;
    c.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
    /* X = 0-80
    AND Y= 0-25*/
}

int main()
{

    int a = 2;
    int *ptr;
    int k=40;
    for (int j = 1; j >= 1; j++)
    {
        system("cls");

        ptr = (int *)calloc(a, sizeof(int));

        gotoxy(k, 13);
        *(ptr + a) = 7;
        // ptr[1] = 0;
        for (int i = 1; i < 2; i++)
        {
            printf("%d",ptr[a]);
        }
        gotoxy(39, 13);
        
        for (int i = 1; i < a; i++)
        {
            printf("%d", ptr[i]);
        }
        a++;
        k++;
        free(ptr);
        getch();
    }
    printf("\nProgram END");
    getch();

    return 0;
}
0 Answers
Related