Thread: Using a function in a function

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    21

    Using a function in a function

    Hello,

    I am havinf some trouble using a function in a function. The original function I am using I know works as it has been tested and is shown here:

    Code:
    void drawCorridorNS(int aPos, int bPos, char **map, int randX, int randY, int a, int b){
    
        int s,t ;
    
        for(s = randY; s <= a; s = s + aPos){
        
            map[randX][s] = ' ';
            
        }
        
        for(t = randX; t <= b; t = t + bPos){
        
            map[t][s] = ' '; //retrace path and change characters to space
            
        }
        
        connected--;
        randLimit = 0;
    
    }
    Now I wish to use this function in another function as shown here:

    Code:
    void lookAlongNS (int direction, int look, char **map, int randX, int randY, int mapSizeX, int a){
    
        int b;
    
        for(b = randX; b < mapSizeX; b++){
            
            if (map[b][a] == WALL){
            //j = a k = b
                if((map[b+1][a] == ' ') || (map[b][a+direction] == ' ')){
                
                    if(direction == 1){
                    
                    drawCorridorNS(1,1,map,randX,randY,a,b);
                    
                    }
                    
                    else{
                    
                    drawCorridorNS(-1,1,map,randX,randY,a,b);
                    
                    }            
                }        
            }
            
            else{
                
                b = mapX;            
            }    
        }
        
        for(b = randX; b < 1; b--){
            
            if (map[b][a] == WALL){
            //j = a k = b
                if((map[b-1][a] == ' ') || (map[b][a+direction] == ' ')){
                
                    if(direction == 1){
                    
                    drawCorridorNS(1,-1,map,randX,randY,a,b);
                    
                    }
                    
                    else{
                    
                    drawCorridorNS(-1,-1,map,randX,randY,a,b);
                    
                    }                
                }        
            }
            
            else{
                
                b = 1;            
            }    
        }
    }
    While this compiles the function is just not working and I was wondering if anyone could see anything I'm doing which isn't allowed.

    Thanks in advance
    Last edited by Sephmk; 03-15-2012 at 10:17 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What are the functions supposed to do, and how does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    21
    The overall algorithm is to draw corridors on a map in a roguelike game, I had written this in the main method but wanted to move it to a funciton. The first function retraces a corridor that has been found and draws it into the map [mapChar]. This works when put in the big chunk of code in the main method. The second one is looking for a corridor and has the exact same logic as the old algorithm but for some reason when using this function it does not work. I cannot tell what the problem is but the program cannot find corridors and if it does retraces them incorrectly.

    My main question is if I need to change any of the inputs to a function since it is being called from within a function

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't think you've given enough information. Why not post the whole thing (after removing the double-spacing).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    21
    The whole program is around 2000 lines and is quite messy =(

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    21
    This is what lookAlongNS comes from:

    Code:
    for(k=randomX;k<mapX;k++){ //First try everything right of the Y co-ordinate
        if(mapChar[k][j] == WALL){ //If the algorithm finds a wall
            if((mapChar[k+1][j] == ' ') || (mapChar[k][j+1] == ' ')){ //if the next character along is an open room then the corridor is acceptable and needs to be printed
                drawCorridorNS(1,1,mapChar,randomX,randomY,j,k);        
                goto southDone;                                
            }//end of seeing if there is space after the wall then printing
            else{
                k = mapX; //if the wall found was not acceptable do not look past it as otherwise the corridor will go through a room
            }
        }//end of seeing if wall was acceptable and printing if it was
    }//end of looking right for wall
    for(k=randomX;k<1;k--){ //Second try everything left of the Y co-ordinate
        if(mapChar[k][j] == WALL){ //If the algorithm finds a wall
            if((mapChar[k-1][j] == ' ') || (mapChar[k][j+1] == ' ')){ //if the next character along is an open room then the corridor is acceptable and needs to be printed
                drawCorridorNS(1,-1,mapChar,randomX,randomY,j,k);
                goto southDone;
                
            }//end of seeing if there is space after the wall then printing
            else{
                k = 1; //if the wall found was not acceptable do not look past it as otherwise the corridor will go through a room
            }                            
        }//end of seeing if wall was acceptable and printing if it was        
    }//end of looking left for wasll

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  2. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  3. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM