Why doesn't this work?

I was making a program that takes a two-dimensional array of chars that can be either '#' or '.' from a text file and makes it work like tetris. '#' are blocks and '.' are empty spaces so if there is a empty space below it moves the block down. This is my program(it's C not C++):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>

int main(void)
{
FILE *ifStr;

ifStr = fopen("input.txt", "r");

char ch[5][5];
int x;
int y;

if(!ifStr) return 0;

for(x = 0;x <= 4;x++)
{

    for(y = 0;y <= 4;y++)
    {
     fscanf(ifStr, "%c", &ch[x][y]);
    }
}


int down = 1;
//
do{
down = 1;
for(x = 0;x <= 4;x++)
{
   for(y = 0;y <= 4;y++)
    {

        if(ch[x][y] == '#' && ch[x + 1][y] == '.')
        {
            ch[x][y] = '.';
            ch[x + 1][y] = '#';
            down = 0;
        }
    }
}
}while(down != 1);
//
FILE *ofStr;

ofStr = fopen("output.txt", "w");

if(!ofStr) return 0;

for(x = 0;x <= 4;x++)
{

    for(y = 0;y <= 4;x++)
    {

        fprintf(ofStr, "%c", ch[x][y]);
    }
    fprintf(ofStr, "\n");
}

fclose(ofStr);
fclose(ifStr);
return 0;
}


Don't mind the empty comments.

It just crashes. Why?
From a real quick look, lines 34 and 37 seem to access and write into out of range memory (x + 1 when x is 4).
I've fixed that. In line 29 I put ;x <= 3; but it still doesn't work. Thanks anyway!
Line 53: for(y = 0;y <= 4;x++)
Thanks!
Topic archived. No new replies allowed.