Why doesn't this work?
Nov 7, 2010 at 1:49pm UTC
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?
Nov 7, 2010 at 2:07pm UTC
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).
Nov 7, 2010 at 2:31pm UTC
I've fixed that. In line 29 I put ;x <= 3;
but it still doesn't work. Thanks anyway!
Nov 7, 2010 at 5:32pm UTC
Line 53: for (y = 0;y <= 4;x++ )
Nov 7, 2010 at 7:01pm UTC
Thanks!
Topic archived. No new replies allowed.