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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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