#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Run-Time Check Failure #2 - Stack around the variable 'temp2' was corrupted.


    Can anyone tell me what "Run-Time Check Failure #2 - Stack around the variable 'temp2' was corrupted." means?
    my code works , the only problem is that after it's completely finished it crashes with the above error.

    this is the code:

    #include <stdio.h>
    #define N 3

    void resetMatrix(int arr[][N], int size);
    void printLine(int size);
    void printRow(int arr[][N],int size);
    void printGame(int arr[][N], int size);
    int check_rows(int arr[][N],int size);
    int check_col(int arr[][N], int size);
    int check_diagonal(int arr [][N],int size);
    int check_game(int arr[][N], int size);
    int check_end(int arr[][N],int size);
    int inputCheck (int arr[][N],int size, int num1 ,int num2);

    //------------------------------------------------------------------

    void resetMatrix(int arr[][N], int size)
    {
    int i,j;
    for(i=0; i<size; i++)
    for(j=0; j<size; j++)
    arr[i][j]=' ';
    }
    void printLine(int size)
    {
    int i;
    printf("+");
    for(i=0; i<size; i++)
    printf("---+");
    printf("\n");
    }
    void printRow(int arr[][N],int size)
    {
    int i,j;
    printf("|");
    for(i=0; i<size; i++)
    for(j=0; j<size; j++)
    printf(" %c |",arr[i][j]);
    printf("\n");
    }
    void printGame(int arr[][N], int size)
    {
    int g,j;
    printf("\n");
    for(g=0; g<size; g++)
    {
    printLine(size);
    printf("|");
    for(j=0; j<size; j++)
    printf(" %c |",arr[g][j]);
    printf("\n");
    }
    printLine(size);
    }
    int check_rows(int arr[][N],int size)
    {
    int i,j,row=0;
    for(i=0; i<size; i++)
    {
    row=0;
    for(j=0; j<size; j++)
    row=row+arr[i][j];
    if(row==(88*size))
    {
    printf("Player 1 wins.\n");
    return 0;
    }
    else if (row==(79*size))
    {
    printf("Player 2 wins.\n");
    return 0;
    }
    }
    return 1;
    }
    int check_col(int arr[][N], int size)
    {
    int i,j,col=0;
    for(j=0; j<size; j++)
    {
    col=0;
    for(i=0; i<size; i++)
    col=col+arr[i][j];
    if(col==(88*size))
    {
    printf("Player 1 wins.\n");
    return 0;
    }
    else if (col==(79*size))
    {
    printf("Player 2 wins.\n");
    return 0;
    }
    }
    return 1;
    }
    int check_diagonal(int arr [][N],int size)
    {
    int i,j,diag=0,diag2=0;
    for(i=0,j=(size-1); i<size; i++,j--)
    {
    diag2=diag2+arr[j][i];
    if(diag2==(88*size))
    {
    printf("Player 1 wins.\n");
    return 0;
    }
    else if(diag2==(79*size))
    {
    printf("Player 2 wins.\n");
    return 0;
    }
    }
    for(j=0; j<size; j++)
    {
    diag=diag+arr[j][j];
    if(diag==(88*size))
    {
    printf("Player 1 wins.\n");
    return 0;
    }
    else if(diag==(79*size))
    {
    printf("Player 2 wins.\n");
    return 0;
    }
    }
    return 1;
    }
    int check_game(int arr[][N], int size)
    {
    if(check_diagonal(arr,size)==0||check_rows(arr,size)==0||check_col(arr,size)==0)
    return 0;
    else return 1;
    }
    int check_end(int arr[][N],int size)
    {
    int i,j,flag=0,flag2=0;
    for(i=0; i<size; i++)
    for(j=0; j<size; j++)
    {
    if(arr[i][j]==' ')
    flag2++;
    else flag++;
    }
    if (flag==(N*N))
    return 0;
    if (flag2>0)
    return 1;
    return 2;
    }
    int inputCheck (int arr[][N],int size, int num1 ,int num2)
    {
    if(num1>(size-1)||num1<0||num2>(size-1)||num2<0||arr[num1][num2]!=' ')
    {
    printf("Wrong input, please try again: ");
    return 0;
    }
    return 1;
    }
    //------------------------------------main----------------------------------------------------
    void main ()
    {
    int matrix[N][N]={0};
    char temp1,temp2,X=88,O=79;
    resetMatrix(matrix, N);
    while (check_end(matrix,N)==1)
    {
    printf("Player 1 turn:\nPlease choose square: ");
    do
    {
    scanf("%d%d",&temp1,&temp2);
    }
    while (inputCheck (matrix, N, temp1, temp2)==0);
    matrix[temp1][temp2]=X;
    printGame(matrix, N);
    if(check_game(matrix, N)==0)
    return;
    if(check_end(matrix, N)==0)
    {
    printf("There is no winner.");
    return;
    }
    printf("Player 2 turn:\nPlease choose square: ");
    do
    {
    scanf("%d%d",&temp1,&temp2);
    }
    while (inputCheck (matrix, N, temp1, temp2)==0);
    matrix[temp1][temp2]=O;
    printGame(matrix, N);
    if(check_game(matrix, N)==0)
    return ;
    if(check_end(matrix,N)==0)
    {
    printf("There is no winner.\n");
    return ;
    }

    }
    }

    Thanks in advance :)
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0
    Never mind , found the problem :)

IMN logo majestic logo threadwatch logo seochat tools logo