Can't figure out what keeps hanging up my program

This is a discussion on Can't figure out what keeps hanging up my program within the C Programming forums, part of the General Programming Boards category; I have a simple program that is setup to create a path labled A-Z. Sometimes it works fine, however the ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    4

    Can't figure out what keeps hanging up my program

    I have a simple program that is setup to create a path labled A-Z. Sometimes it works fine, however the default statement on the switch keeps getting accessed and I don't know why. Also sometimes the program just hangs like its in an endless loop. Any help would be great.

    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    #define N 20
    #define ALPHA 26
    using namespace std;
    
    int main(void)
    {
    	srand((unsigned) time(NULL));
    	int step, row, col;
    	char path [N][N];
    
    	for (;;)
    	{
    /**** Section 1: Initializes the array to have character '.' ****/
    	for (row = 0; row < N; row++)
    	{
    		for (col = 0; col < N; col++)
    			path [row][col]='.';
    	}
    /**** End Section 1 ****/
    
    /**** Section 2: Selects the path using character Capital Letters ****/
    	path [row = rand() % N] [col = rand() % N]= 'A';
    	for (step = 1; step < ALPHA;)
    	{
    		if (path [row +1][col] != '.' && path [row -1][col] != '.' 
    			&& path [row][col +1] != '.' && path [row][col -1] != '.')
    			break;
    		switch (rand() % 4)
    		{
    			case 0: 
    				if (path [row + 1][col] =='.' && (row + 1) >= 0 && (row + 1) < N)
    				{
    					row++;
    					path [row] [col] = 'A' + step;
    					step++;
    				}
    				else
    				{
    					break;
    				}
    			case 1: 
    				if (path [row - 1][col] =='.' && (row - 1) >= 0 && (row - 1) < N)
    				{
    					row--;
    					path [row] [col] = 'A' + step;
    					step++;
    				}
    				else
    				{
    					break;
    				}
    			case 2: 
    				if (path [row][col + 1] =='.' && (col + 1) >= 0 && (col +1) < N)
    				{
    					col++;
    					path [row] [col] = 'A' + step;
    					step++;						
    				}
    				else
    				{
    					break;					
    				}
    			case 3:
    				if (path [row][col - 1] =='.' && (col - 1) >= 0 && (col -1) < N)
    				{
    					col--;
    					path [row] [col] = 'A' + step;
    					step++;
    				}
    				else
    				{
    					break;
    				}
    			default: printf("something is wrong here %d\n", step);
    		}
    	}
    
    /**** End Section 2 ****/
    
    /**** Section 3: Prints the path using character '.' ****/
    	for (row = 0; row < N; row++)
    	{
    		for (col = 0; col < N; col++)
    			printf("%c ",path [row][col]);
    		printf("\n");
    	}
    /**** End Section 3 ****/
    
    	system("pause");
    	}
    	return(0);
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Why are the breaks inside else statements? Do you ever want it to go from one case straight on to the next?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    switch cases have fallthrough behavior. If you do not explicitly break form the switch, then code will continue running until the end of the switch statement. try this to demonstrate the problem:

    Code:
            switch (rand() &#37; 4)
            {
                case 0: 
                    if (path [row + 1][col] =='.' && (row + 1) >= 0 && (row + 1) < N)
                    {
                        row++;
                        path [row] [col] = 'A' + step;
                        step++;
                    }
                    else
                    {
                        break;
                    }
                    puts("case 0");
                case 1: 
                    if (path [row - 1][col] =='.' && (row - 1) >= 0 && (row - 1) < N)
                    {
                        row--;
                        path [row] [col] = 'A' + step;
                        step++;
                    }
                    else
                    {
                        break;
                    }
                    puts("case 1");
                case 2: 
                    if (path [row][col + 1] =='.' && (col + 1) >= 0 && (col +1) < N)
                    {
                        col++;
                        path [row] [col] = 'A' + step;
                        step++;                        
                    }
                    else
                    {
                        break;                    
                    }
                    puts("case 2");
                case 3:
                    if (path [row][col - 1] =='.' && (col - 1) >= 0 && (col -1) < N)
                    {
                        col--;
                        path [row] [col] = 'A' + step;
                        step++;
                    }
                    else
                    {
                        break;
                    }
                    puts("case 3");
                default: printf("something is wrong here %d\n", step);
                    puts("default");
            }

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    4

    Perfect

    Thanks,

    I added breaks into each case statement and that seemed to solve that problem. Seems to work like a charm, however everyonce in a while it still hangs up. Here is the slightly modified code.

    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    #define N 20
    #define ALPHA 26
    using namespace std;
    
    int main(void)
    {
    	srand((unsigned) time(NULL));
    	int step, row, col;
    	char path [N][N];
    
    	for (;;)
    	{
    /**** Section 1: Initializes the array to have character '.' ****/
    	for (row = 0; row < N; row++)
    	{
    		for (col = 0; col < N; col++)
    			path [row][col]='.';
    	}
    /**** End Section 1 ****/
    
    /**** Section 2: Selects the path using character Capital Letters ****/
    	path [row = rand() % N] [col = rand() % N]= 'A';
    	for (step = 1; step < ALPHA;)
    	{
    		if (path [row +1][col] != '.' && path [row -1][col] != '.' 
    			&& path [row][col +1] != '.' && path [row][col -1] != '.')
    			break;
    		switch (rand() % 4)
    		{
    			case 0: 
    				if (path [row + 1][col] =='.' && (row + 1) >= 0 && (row + 1) < N)
    				{
    					row++;
    					path [row] [col] = 'A' + step;
    					step++;
    				}
    				else
    				{
    					break;
    				}
    				break;
    			case 1: 
    				if (path [row - 1][col] =='.' && (row - 1) >= 0 && (row - 1) < N)
    				{
    					row--;
    					path [row] [col] = 'A' + step;
    					step++;
    				}
    				else
    				{
    					break;
    				}
    				break;
    			case 2: 
    				if (path [row][col + 1] =='.' && (col + 1) >= 0 && (col +1) < N)
    				{
    					col++;
    					path [row] [col] = 'A' + step;
    					step++;						
    				}
    				else
    				{
    					break;					
    				}
    				break;
    			case 3:
    				if (path [row][col - 1] =='.' && (col - 1) >= 0 && (col -1) < N)
    				{
    					col--;
    					path [row] [col] = 'A' + step;
    					step++;
    				}
    				else
    				{
    					break;
    				}
    				break;
    			default: 
    				printf("something is wrong here %d\n", step);
    				break;
    		}
    	}
    
    /**** End Section 2 ****/
    
    /**** Section 3: Prints the path using character '.' ****/
    	for (row = 0; row < N; row++)
    	{
    		for (col = 0; col < N; col++)
    			printf("%c ",path [row][col]);
    		printf("\n");
    	}
    /**** End Section 3 ****/
    
    	system("pause");
    	}
    	return(0);
    }

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    4

    Got rid of the else statements

    oh I also got rid of the else statements since they were redundant. Thanks for that heads up as well.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    what do you want it to do and what does it do?

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    4

    Purpose

    All I am looking for is to print out a random path from A to Z one step at a time across a two dimensional array, which it does now. It seems to do exactly what I want most of the time. However sometimes it does not display the data, instead all i get is an unresponsive blinking cursor. I am trying to figure out what causes that.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    if (path [row + 1][col] =='.' && (row + 1) >= 0 && (row + 1) < N)
    the range check should be first, before the access.
    Code:
    if ((row + 1) >= 0 && (row + 1) < N && path [row + 1][col] =='.')
    also, why not put the step increment outside the switch case?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me figure this out for class, important.
    By WinterInChicago in forum C++ Programming
    Replies: 19
    Last Post: 10-18-2006, 05:18 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21