What was wrong with this code??

This is a discussion on What was wrong with this code?? within the C Programming forums, part of the General Programming Boards category; Hi all, I have two piece of code, but i am not sure where did it go work, it just ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    49

    What was wrong with this code??

    Hi all,
    I have two piece of code, but i am not sure where did it go work, it just didn't work out the way it want to be.Can anyone help me to troubleshoot.

    This is one part of my code.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #include "navigation.h"
    #include "robot.h"
    
    
    // Usage: ./readMaze <mazefile.txt> <movelimit>
    int main(int argc, char **argv){
        FILE *f;
        int w,h;
        int i,j;
       
        int x,y;
       
        char **maze;
        char *buf;
       
        int moves=0;
        int maxMoves=0;
        int won=0;
    
        char *name=robot_name();
    
        maxMoves = atoi(argv[2]);
    
        f=fopen(argv[1],"r");
        fscanf(f,"%dx%d\n",&w,&h);
       
        maze=(char**)malloc(sizeof(char)*h);
        for (i=0;i<h;i++){
            maze[i]=(char*)malloc(sizeof(char)*w);
        }
        buf = (char*) malloc(sizeof(char)*w);
       
        for (i=0;i<h;i++){
            fgets(buf,w,f);
            for (j=0;j<w;j++){
                if (buf[j]=='X'){
                    y=j;
                    x=i;
                    maze[i][j]='.';
                }else{
                    maze[i][j]=buf[j];
                }
            }
        }
        fclose(f);
       
        for (i=0;i<h;i++){
            for (j=0;j<w;j++){
                fprintf(stderr,"%c",maze[i][j]);
            }
            fprintf(stderr," - that was line %d\n",i);
        }
        fprintf(stderr,"$ done reading map\n");
    
        start_at(x,y);
       
        // assume the maze is surrounded by walls
        //so we're always 1 away from the edge
        while (moves < maxMoves && (!won)){
            Wallinfo w;
            enum Dir a;
           
            // Determine what's blocked
           
            fprintf(stderr,"$ -----------------------\n");
            fprintf(stderr,"$  (%s) at (%d,%d)\n",name,x,y);
           
            if (maze[y-1][x]=='#')  w.N = 1;
            else                    w.N = 0;
           
            if (maze[y+1][x]=='#')  w.S = 1;
            else                    w.S = 0;
           
            if (maze[y][x+1]=='#')  w.E = 1;
            else                    w.E = 0;
           
            if (maze[y][x-1]=='#')  w.W = 1;
            else                    w.W = 0;
           
            a = robot_move(&w);
            moves++;
    
            if (a==N){
                if(maze[y-1][x]=='#'){
                    fprintf(stderr,"$ North is blocked!\n");
                    continue;
                } else {
                    y--;
                    fprintf(stderr, "$ (%s) moved N\n",name);
                    if (maze[y][x]=='E') won=1;
                    continue;
                }
            }
            if (a==S){
                if (maze[y+1][x]=='#'){
                    fprintf(stderr,"$ South is blocked!\n");
                    continue;
                } else {
                    y++;
                    fprintf(stderr,"$ (%s) moved S\n",name);
                    if (maze[y][x]=='E') won=1;
                    continue;
                }
            }
            if (a==E){
                if(maze[y][x+1]=='#'){
                    fprintf(stderr,"$ East is blocked!\n");
                    continue;
                } else {
                    x++;
                    fprintf(stderr,"$ (%s) moved E\n",name);
                    if (maze[y][x]=='E') won=1;
                    continue;
                }
            }
            if (a==W){
                if(maze[y][x-1]=='#'){
                    fprintf(stderr,"$ West is blocked!\n");
                    continue;
                } else {
                    x--;
                    fprintf(stderr,"$ (%s) moved W\n",name);
                    if (maze[y][x]=='E') won=1;
                    continue;
                }
            }
           
        }
       
        if (won)
            fprintf(stderr,"$ (%s) won in %d moves!\n",name,moves);
        else
            fprintf(stderr,"$ (%s) didn't win!\n",name);
        return 0;
    }
    This is another part
    Code:
    #include "navigation.h"
    #include "robot.h"
    
    int found_wall=1;
    enum Dir hand_on_wall;
    
    char *my_name = "Wallbot 0.1";
    
    char *robot_name() { return my_name; }
    
    void start_at(int x,int y){
        return;
    }
    
    enum Dir robot_move(Wallinfo *w){
        int decided=0;
        enum Dir result;
    
        if (!found_wall){
            if (w->N){
                hand_on_wall=N;
            } else {
                return N;
            }
        }
    
        // Walk clockwise - If hand is on north wall, should try to go north first
        // (eg. corners)
        while (!decided){
            if (hand_on_wall==N){
                if (!(w->N)){
                    decided=1;
                    hand_on_wall=W;
                    result=N;
                } else
                if (!(w->W)){
                    decided=1;
                    result=E;
                } else {
                    hand_on_wall=E;
                }
            } else
            if (hand_on_wall==E){
                if (!(w->E)){
                    decided=1;
                    hand_on_wall=N;
                    result=E;
                } else
                if (!(w->S)){
                    decided=1;
                    result=S;
                } else {
                    hand_on_wall=S;
                }
            } else
            if (hand_on_wall==S){
                if (!(w->S)){
                    decided=1;
                    hand_on_wall=E;
                    result=S;
                } else
                if (!(w->W)){
                    decided=1;
                    result=W;
                } else {
                    hand_on_wall=W;
                }
            } else
            if (hand_on_wall==W){
                if (!(w->W)){
                    decided=1;
                    hand_on_wall=S;
                    result=W;
                } else
                if (!(w->N)){
                    decided=1;
                    result=N;
                } else {
                    hand_on_wall=N;
                }
            }
        }
        return result;
    }
    Thanks in advance.
    diana --> programming is tough

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    So what's wrong with it? In what way is it not working as expected?

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    let say i have a maze like this.

    Code:
    6x6
    #E####
    #.#.##
    #...##
    ###.##
    ##X.##
    ######
    when i run the code it will give me segmentation fault.
    diana --> programming is tough

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    the headers you created must be in the same directory of your program, or you must give all the way to arrive to them, have you tried that?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Ok. The next thing you need to do is step through your code with a debugger to figure out which line is giving you the seg fault.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    that ussually means you're trying to assign something to memory but there is no space allocated for it..

    print out messages at key points in your code to narrow down where there error is at...
    without looking at your code i'm guessing it's going to be a loop; either the very first iteration, or the last iteration...
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    Yap my header files is in the same folder as my all other files.

    This is one robot2 header file
    Code:
    #include "navigation.h"
    
    char *robot_name();
    void start_at(int x,int y);
    enum Dir robot_move(Wallinfo *info);
    And this is my navigation header files
    Code:
    // Common language for finding your way around a maze, spoken by the server and robot
    
    
    #ifndef NAVIGATION_H
    #define NAVIGATION_H
    
    enum Dir {N,S,E,W};
    
    // 1 or 0
    typedef struct {
        int N,S,E,W;
    } Wallinfo;
    #endif
    Ok. The next thing you need to do is step through your code with a debugger to figure out which line is giving you the seg fault.
    How can i use a debugger to check my program. Another thing is that i am using a makefile to compile all my program.
    Last edited by dianazheng; 10-25-2004 at 12:45 AM.
    diana --> programming is tough

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    Does anyone have any solutions or somthings about this error...
    diana --> programming is tough

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Noone is going to debug your program for you. It's about time you learn some debugging techniques.

    You can easily debug your own program by simply adding some printf statements, while flusing the output stream after each one. If furthermore, you can make it (buzzword alert) "interactive" with the addition of a getchar call after each.

    You could always step through it on paper...

    You could learn how to use something like GDB...

    You could...

    Well, you get the idea.

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

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    If every helper is like you, no one will ask question in a forum....
    diana --> programming is tough

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    Sorry i wasn't meant to be rude or what, but i feel that if every helper here is like you, no one will ask questions or ask for help in a forum.... Because all they get fort he reply will be, you should have learn everything by yourself or you can buy a book...
    diana --> programming is tough

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well if noone asked, then they'd be figuring it out for themselves, wouldn't they? Did you ever read the Announcements? We don't do everything for you here. You asked if anyone had a solution, and I gave you the solution. You just dont' like the solution, which is to actually do something for yourself.

    How on earth do you ever propose to program something "for real", if you expect everyone to do everything for you? I gave you multiple ways how you can fix your own problem. That's not good? That's not what you want? You want to rely on everyone else to fix your own mistakes?

    What am I saying? Of course that's what you want.

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

  13. #13
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    you could give me a mulitple solutions, that might not be the solution that i wanted but al least i would know that, oh they are so actually so many ways to do one thing and maybe from the solution, i might think of something that i never thought of or miss it some way.

    Maybe the way i ask my question was not approperiate.
    diana --> programming is tough

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Placing printf statements throughout your code is one of the easiest ways to debug your code on your own there is. VERY simple. What I was trying to get across is that you should try it yourself, and here are some ways to do it.

    When you place a whole mass of code out there, there aren't a lot of people who will go through it line by line for you just because they're feeling generous. Thus, if you can learn how to do it yourself, you'll get your answer a whole lot faster than waiting around for the mood to strike someone.

    Consider using something like:
    Code:
    #define DEBUG(x) printf("Debug: line %d, %s\n", __LINE__, (x) ); fflush( stdout );
    You can customize this to your liking, but in the above example, maybe we'd do:
    Code:
    DEBUG( "Inside second loop, counting array cells." );
    As stated, if you want it to pause then, you could add a getchar call to that macro line, or whatever.

    Another example would be something like this:
    Code:
    printf("Debugging inner loop: x is %d, y is %d\n", x, y );
    Often times when using printf-debugging, I'll leave the debug lines unindented, so that they're to the far left, which makes for easy removal when I'm done with debugging.

    Again, the point is, it's very easy to debug your program on your own, and faster usually, than having someone take the time to do it for you. Plus, you're likely to learn a whole lot more doing it yourself.

    Teach a man to fish...

    [edit=waiting...] The C Board apparently died before I could hit the 'Post Quick Reply' button, so this may be an irrelevant post by the time it actually gets there... (So now, hours later, you get this reply!) [/edit]

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

  15. #15
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    I try using gdb to debug but i couldn't get anything to print out, did i miss out something somewhere.

    This were the steps:
    Code:
    $ ./solvemaze1 ulimit -c unlimited
    Segmentation fault (core dumped)
    
    $ gdb solvemaze1 core
    GNU gdb 6.0-2mdk (Mandrake Linux)
    Copyright 2003 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i586-mandrake-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".
    
    /home/diana/2041lab/ass2/task1/try/core: No such file or directory.
    (gdb) print y
    No symbol "y" in current context.
    What does this means.
    diana --> programming is tough

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 08:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 01:51 AM

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