Creating dimensions of a 'block' from a file
Can't figure this out...it's pretty simple really, all I want to do is have a struct of "blocks" with x, y, top, bottom and whatnot. It supposed to read from a txt file which has '.' characters wich represent space, and '*' characters, which represent the block. For example, This block would start at 2 and end at 5:
..***.
(just trust me). it would actaully be 20 and 50 since I'm doing everything in 10's. I have the code written, and everything is all happy, but for some reason I'm having some little...*ehem*...uninitialized problem (at least that's what it seems to be):
Code:
#include <iostream.h>
#include <fstream.h>
const int numBlocks = 2;
struct BLOCK
{
BLOCK::BLOCK()
{
x = 0;
y = 0;
w = 0;
h = 10;
left = 0;
right = 0;
bottom = y;
top = bottom - h;
}
double x, y;
double w, h;
double left, right;
double top, bottom;
};
int main()
{
int i = 0;
int k = 0;
BLOCK block[numBlocks];
ifstream inFile;
char ch[11][11];
int r, c;
bool end = false;
int numstars = 0;
inFile.open("level.txt", ios::nocreate);
if (!inFile.fail())
{
i = 0;
for (r = 0; r <= 10; r++)
{
for (c = 0; c <= 10; c++)
{
inFile>>ch[r][c];
if (!inFile.eof())
block[i].y = (r * 10) + 10;
if (ch[r][c] == '.')
{
if (end == true)
{
block[i].right = block[i].left + (numstars * 10);
numstars = 0;
i++; //this is causing the problem!
}
if (end == false)
block[i].left += 10;
end = false;
if (c > 0 && c % 10 == 0)
block[i].left = 0;
}
else if(ch[r][c] == '*')
{
numstars++;
end = true;
}
}
}
}
inFile.close();
for (k = 0; k < numBlocks; k++)
{
block[k].bottom = block[k].y;
block[k].top = block[k].bottom - block[k].h;
block[k].x = block[k].left;
/*output*/
cout<<"block["<<k<<"].bottom = "<<block[i].bottom<<endl;
cout<<"block["<<k<<"].top = "<<block[i].top<<endl;
cout<<"block["<<k<<"].left = "<<block[i].left<<endl;
cout<<"block["<<k<<"].right = "<<block[i].right<<endl;
cout<<endl<<endl;
}
return 0;
}
When i increases, it doesn't work at all (all the ouput is just what was set in the constructor). If you comment out:
it will work, but will only do one block. The rest is pretty self explanatory I think. By the way, you always need to have one '.' as the last character just because of how it works.
Thanks in advance.:D