Thread: Creating dimensions of a 'block' from a file

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    Thumbs down 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:

    Code:
    i++;
    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.
    Last edited by funkydude9; 07-02-2003 at 02:46 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    Question

    Originally posted by Salem

    It's pretty clear that i steps off the end of the array into no-mans land

    Try something along the lines of
    Code:
    inFile.open("level.txt", ios::nocreate);
    if (!inFile.fail()) {
        read_file( inFile, ch, 11, 11 );
        find_blocks( ch, 11, 11, block, numBlocks );
        print_blocks( block, numBlocks );
    }
    I'm not sure what you mean. Where exactly does i step off into no-mans land?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    I did, It doesn't. It's 0 for the first few times, then becomes 1. That's it. Are you sure you're not getting confused with the variables r and c?
    Last edited by funkydude9; 07-02-2003 at 03:36 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    What array are we talking about here? ch[][] or block[]?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by funkydude9
    What array are we talking about here? ch[][] or block[]?
    When in doubt, provide more information to the would-be helpers. For example, the example file you're reading from.

    You could of course add your own debugging lines:

    std::cout << "Foo is " << foo << endl;;

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    I'm still at a lost. I don't know what I should do.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    $%!@#*!

    It's hard to seperate the read_file code and find_blocks code since they kind of work one in one. But I tried best as I could to come up with those functions, after adding a quite a few more parameters to each, except I have to call find_blocks inside of read_file to make it as simple as possible. I don't see why I need debug output for ch[][], I know that works fine. The left side now gets set, but the right side never does, and the left is the same for both if there is more then one block. I don't get it...on one hand, I don't have everything completly set to 0 like before, but on the other, the right side of the block now doesn't get set. Argh! This is driving me crazy, for the life of me I can't figure out what I'm doing wrong. It acting like i never gets increased (but it does!). This is what it's looking like:

    Code:
    if (!inFile.fail())
    	{
    		readFile(inFile, ch, 11, 11, block);
    		print_ch(ch, 11, 11);
    	}
    
    	inFile.close();
    ...

    Code:
    void readFile(ifstream &inFile, char ch[][11], int numRows, int numCols, BLOCK block[]) 
    {
    	int r, c;
    	int i = 0;
    
    	for (r = 0; r < numRows; r++)
    	{
    		for (c = 0; c < numCols; c++)
    		{
    			if (!inFile.eof())
    				inFile>>ch[r][c];
    
    			findBlocks(ch, 11, 11, block, inFile, r ,c, i);
    		}
    	}
    }
    
    void findBlocks(char ch[][11], int numRows, int numCols, BLOCK block[], ifstream &inFile, int r, int c, int &i) 
    {	
    	
    	bool end = false;
    	int numstars = 0;
    
    
    	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!
    			block[i].left = 0; //LOOK! left gets set back to 0! (which I don't even need to do!) But both left's are STILL the SAME in the output!
    		}
    
    		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;
    		
    	
    	}
    }
    Last edited by funkydude9; 07-03-2003 at 12:11 PM.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    I've been running it with all kinds of tests. Now I have a zip attached, with 2 .cpp files, and 1 .txt file. Read the header comments to see the differences (one is with the functions, and the other is without). Hope this helps clarify the problems alittle better Salem.

    BTW, what's up with all these login problems? It tells me *sometimes* that I need to enter a username when I'm trying to post even though I'm already logged in, and why don't you guys have cookies set up?
    Last edited by funkydude9; 07-03-2003 at 01:27 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    *knocks head on wall* Wow, that was so stupid. I was getting k and i messed up in the output. Crap! Ok...let me figure this out...

    ::EDIT::

    Nm, I think it's working now...wow that was dumb. sorry guys.
    Last edited by funkydude9; 07-03-2003 at 02:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM