Thread: Weird problem

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    23

    Weird problem

    Here is my input for this code.

    1
    11111111111111111111
    11111111111111111111
    1111S000G11111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111


    The problem.

    When checking for char 'S' the array is showing it is finding S at element [2][7] this is obviously incorrect.

    Code:
    FILE *infile; // file 
    	
    		// If statement for opening file and error for fail.
    		if((infile = fopen(file, "r")) == NULL)
    		{
    			printf("Error Opening File. \n");
    			exit(1);
    		}
    
    	fscanf(infile, "%d", &num_of_mazes);
    	
    	char **maze;
    	maze = (char **) malloc(20 * sizeof(char *));
    	
    		if(maze == NULL)
    		{
    			printf("ERROR cannot be allocated");
    		}
    		
    		for(i = 0; i < 20; i++)
    		{
    		
    		maze[i] = (char *) malloc(20 * sizeof(char));
    			
    		}
    	
    		
    		for(k = 0; k < 20; k++)
    		{		
    			for(j = 0; j < 20; j++)
    			{
    			
    			fscanf(infile, "%c", &maze[k][j]);
    			
    			
    			
    			}
    			
    		}
    	
    	
    	for(k = 0; k < 20; k++)
    		{
    			for(j = 0; j < 20; j++)
    			{
    				printf("%c", maze[k][j]);
    				
    				if(maze[k][j] ==  'S')
    				{
    					printf("%d %d", k, j);
    				}
    			}
    		}
    Im not sure if I didn't allocate right.. or if my scanf is getting bugged. Anyone have any ideas?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You shouldn't cast malloc().

    Code:
    fscanf(infile, "%c", &maze[k][j]);
    Try switching the [k] and [j], maybe?

    Your memory looks allocated properly (but it isn't freed).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    Nope switching it isnt the problem, products output like so.

    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    111111111111111S7 21111
    11111111111111011111
    11111111111110111111
    11111111111101111111
    11111111111G11111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111
    11111111111111111111

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    sizeof(char)
    is always 1, you don't need it in there.

    Try this:
    Code:
    	for(k = 0; k < 20; k++)
    		{
    			for(j = 0; j < 20; j++)
    			{
    				printf("%c", maze[k][j]);
    				
    				if(maze[k][j] ==  'S')
    				{
    					printf("%d %d", k, j);
    				}
    			}
    
                            printf("\n");
    		}
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    fscanf(infile, "%c", &maze[k][j]);
    will read the lineterminater '\n' as well. This messes up your line and column indexes.
    Kurt

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    using this code:
    Code:
    	char **maze;
    	maze = (char **) malloc(20 * sizeof(char *));
    	
    		if(maze == NULL)
    		{
    			printf("ERROR cannot be allocated");
    		}
    		
    		for(i = 0; i < 20; i++)
    		{
    		
    		maze[i] = (char *) malloc(20);
    			
    		}
    	
    		
    		for(k = 0; k < 20; k++)
    		{		
    			for(j = 0; j < 20; j++)
    			{
    			
    			fscanf(infile, "%c", &maze[k][j]);
    			
    			
    			
    			}
    			
    		}
    	
    for(k = 0; k < 20; k++) 
    		{ 
    			for(j = 0; j < 20; j++) 
    				{ 
    				printf("%c", maze[k][j]); 
    				if(maze[k][j] == 'S') 
    				{ printf("%d %d", k, j);
    				 }
    			 } printf("\n");
    		 }
    I get this output.

    1111111111111111111
    1
    111111111111111111
    11
    1111S2 7000G1111111
    111
    1111111111111111
    1111
    111111111111111
    11111
    11111111111111
    111111
    1111111111111
    1111111
    111111111111
    11111111
    11111111111
    111111111
    1111111111
    1111111111
    111111111
    11111111111
    11111111
    111111111111
    1111111
    1111111111111
    111111
    11111111111111
    11111
    111111111111111
    1111
    1111111111111111
    111
    11111111111111111
    11
    111111111111111111
    1
    1111111111111111111



    as you can see, its still recognizing the 'S' as element 7.. what the heck.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    		for(k = 0; k < 20; k++)
    		{		
    			for(j = 0; j < 20; j++)
    			{
    			fscanf(infile, "%c", &maze[k][j]);
    			}			
    		}
    The problem is there's a newline character at the end of the line, so you are reading that also. So you could add an extra fscanf() call after the first loop:
    Code:
    		for(k = 0; k < 20; k++)
    		{		
    			for(j = 0; j < 20; j++)
    			{
    			fscanf(infile, "%c", &maze[k][j]);
    			}			
    			fscanf(infile, "%*c");
    		}
    Or you could read a line with fgets(), then scan with sscanf(), and you wouldn't need the extra call to sscanf().
    Last edited by swoopy; 02-17-2006 at 01:42 PM. Reason: Fixed fscanf so it's not writing to out-of-bounds memory

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    Haha thanks!

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    You can just put a \n in your scanfs

    Code:
    fscanf(infile, "%d\n", &num_of_mazes);
    fscanf(infile, "%c\n", &maze[k][j]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird Problem With Pointer
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2007, 07:50 PM
  2. Weird problem on '02 3.4L V6 auto
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-12-2006, 12:05 AM
  3. Really Weird itoa Problem
    By Grantyt3 in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 12:44 AM
  4. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  5. Weird class problem!
    By aker_y3k in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2002, 06:12 AM