help finishing this program

This is a discussion on help finishing this program within the C Programming forums, part of the General Programming Boards category; the program is a 2d floor plan navigating program which displays which room you are currently in, a description of ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    6

    help finishing this program

    the program is a 2d floor plan navigating program which displays which room you are currently in, a description of the room and available exits for n, e, s, w directions.

    the program runs okay until you hit a direction (n, e, s, w key) and press enter.

    any idea why its not working 100%?

    help is greatly appreciated guys thanks in advance

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <conio.h>
    
    
    #define North 0
    #define East 1
    #define South 2
    #define West 3
    
    
    typedef struct
    {
       char Description[200];
       unsigned int Exit[4];
    } Location;
    
    
    Location Room[8];
    
    
    void RoomSetup(void)
    {
       strcpy(Room[1].Description, "Entrance Corridor");
       Room[1].Exit[North]=6;
       Room[1].Exit[East]=2;
       Room[1].Exit[South]=0;
       Room[1].Exit[West]=3;
    
    
       strcpy(Room[2].Description, "Living Room");
       Room[2].Exit[North]=4;
       Room[2].Exit[East]=0;
       Room[2].Exit[South]=0;
       Room[2].Exit[West]=1;
    
    
       strcpy(Room[3].Description, "Garage");
       Room[3].Exit[North]=5;
       Room[3].Exit[East]=1;
       Room[3].Exit[South]=0;
       Room[3].Exit[West]=0;
    
    
       strcpy(Room[4].Description, "Dining Room");
       Room[4].Exit[North]=7;
       Room[4].Exit[East]=0;
       Room[4].Exit[South]=2;
       Room[4].Exit[West]=0;
    
    
       strcpy(Room[5].Description, "Spare Room");
       Room[5].Exit[North]=0;
       Room[5].Exit[East]=0;
       Room[5].Exit[South]=3;
       Room[5].Exit[West]=0;
    
    
       strcpy(Room[6].Description, "Kitchen");
       Room[6].Exit[North]=8;
       Room[6].Exit[East]=7;
       Room[6].Exit[South]=1;
       Room[6].Exit[West]=0;
    
    
       strcpy(Room[7].Description, "Pantry");
       Room[7].Exit[North]=0;
       Room[7].Exit[East]=0;
       Room[7].Exit[South]=4;
       Room[7].Exit[West]=5;
    
    
       strcpy(Room[8].Description, "Patio");
       Room[8].Exit[North]=0;
       Room[8].Exit[East]=0;
       Room[8].Exit[South]=5;
       Room[8].Exit[West]=0;
    
    
    }
    
    
    void DescribeRoom(unsigned int RoomNumber)
    {
       printf("Welcome to the home of James Ward's Family.\n\n");
       printf("You are currently in the %s. ", Room[RoomNumber].Description);
    }
    
    
    void DescribeExits(int RoomNumber)
    {
       unsigned int exitdir;
       unsigned int exitcount = 0;
       for (exitdir = 0; exitdir < 4; exitdir++)
       {
          if (Room[RoomNumber].Exit[exitdir] > 0) exitcount++;
       }
       if (exitcount == 1) printf("This room has %u exit: \n", exitcount);
       else printf("This room has %u exits: \n", exitcount);
       if (Room[RoomNumber].Exit[0] > 0) printf("   North: %s\n", Room[Room[RoomNumber].Exit[0]].Description);
       if (Room[RoomNumber].Exit[1] > 0) printf("   East:  %s\n", Room[Room[RoomNumber].Exit[1]].Description);
       if (Room[RoomNumber].Exit[2] > 0) printf("   South: %s\n", Room[Room[RoomNumber].Exit[2]].Description);
       if (Room[RoomNumber].Exit[3] > 0) printf("   West:  %s\n", Room[Room[RoomNumber].Exit[3]].Description);
       printf("\n");
    }
    
    
    void GetDirection(char Direction[])
    {
       printf("Type the first letter of the direction you want to go in, or type q to quit\n");
       scanf("%s", Direction);
       while(strlen(Direction) > 1)
       {
          printf("You may only type ONE letter - please try again\n");
          scanf("%s", Direction);
       }
    }
    
    
    int main(void)
    {
       char Direction[1];
       char DummyInput[1];
       char ExitName[5];
       unsigned int RoomNumber = 1;
       unsigned int NewRoomNumber, i, CheckExit, ValidExit;
       RoomSetup();
       while(1){
          system("cls"); //Clear Screen
          DescribeRoom(RoomNumber); //Describe current Room
          DescribeExits(RoomNumber); //Describe current Room Exits
          GetDirection(Direction); //Get direction to go in
          if (strcmp(Direction, "Q") == 0 || strcmp(Direction, "q") == 0) exit(0);
          ValidExit = 1;
          if (strcmp(Direction, "N") == 0 || strcmp(Direction, "n") == 0)
          {
             CheckExit = 0;
             strcpy(ExitName, "North");
          }
          else if (strcmp(Direction, "E") == 0 || strcmp(Direction, "e") == 0)
          {
             CheckExit = 1;
             strcpy(ExitName, "East");
          }
          else if (strcmp(Direction, "S") == 0 || strcmp(Direction, "s") == 0)
          {
             CheckExit = 2;
             strcpy(ExitName, "South");
          }
          else if (strcmp(Direction, "W") == 0 || strcmp(Direction, "w") == 0)
          {
             CheckExit = 3;
             strcpy(ExitName, "West");
          }
          else
          {
             ValidExit = 0;
             printf("'%s' is not a valid direction - please press any key and 'Enter' to continue\n", Direction);
             scanf("%s", DummyInput);
          }
          if (ValidExit == 1)
          {
             if (Room[RoomNumber].Exit[CheckExit] > 0) RoomNumber = Room[RoomNumber].Exit[CheckExit];
             else
             {
                printf("There is no exit in the %s - please press any key and 'Enter' to continue\n", ExitName);
                scanf("%s", DummyInput);
             }
          }
       }
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    65
    Quote Originally Posted by j.ward91 View Post
    the program is a 2d floor plan navigating program which displays which room you are currently in, a description of the room and available exits for n, e, s, w directions.

    the program runs okay until you hit a direction (n, e, s, w key) and press enter.

    any idea why its not working 100%?

    help is greatly appreciated guys thanks in advance

    Code:
    typedef struct
    {
       char Description[200];
       unsigned int Exit[4];
    } Location;
    
    
    Location Room[8];
    
    
    void RoomSetup(void)
    {
       /***** C ARRAYS start at 0 , 8 is past the end of the array ***/
       strcpy(Room[8].Description, "Patio");
       Room[8].Exit[North]=0;
       Room[8].Exit[East]=0;
       Room[8].Exit[South]=5;
       Room[8].Exit[West]=0;
    
    
    }
    C ARRAYS start at 0 , 8 is past the end of the array

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    fixed that error, but same problem still persists, thanks for the fix though

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    There's a lot of convoluted/unnecessary/confusing code here.

    You're going out of bounds with your room array, it's 0-7, not 1-8. This effectively skips the first element, while overwriting memory after the last element.

    Your GetDirection function is not only inefficient, but any buffer overflows that your (incorrectly used) while loop is designed to prevent, will have already happened in the scanf(). Instead, why not pass a pointer to a single char, and use getchar(). Then, convert it to lower case, and skip all the strcmp() tish by just comparing the character. For example:
    Code:
    void get_direction (char *direction)
    {
        int c = getchar();
        (*direction) = c | 0x20; /* lower case */
    }
    /* ... */
    char direction;
    get_direction (&direction);
    
    if (direction == 'w') {
        /* ... */
    You should use #defines instead of magic numbers for the rooms.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    65
    Quote Originally Posted by j.ward91 View Post
    fixed that error, but same problem still persists, thanks for the fix though
    you can be running past the end of the array direction since it only has one char.
    you'll need to use a value other than 0 for a bad exit if you have a room 0.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by j.ward91 View Post
    fixed that error, but same problem still persists, thanks for the fix though
    You have neither shown how you fixed that, nor actually told us what really happens when it goes wrong.
    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"

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Quote Originally Posted by iMalc View Post
    You have neither shown how you fixed that, nor actually told us what really happens when it goes wrong.
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <conio.h>
    
    
    #define North 0
    #define East 1
    #define South 2
    #define West 3
    
    
    typedef struct
    {
       char Description[200];
       unsigned int Exit[4];
    } Location;
    
    
    Location Room[0];
    
    
    void RoomSetup(void)
    {
       strcpy(Room[0].Description, "Entrance Corridor");
       Room[0].Exit[North]=5;
       Room[0].Exit[East]=1;
       Room[0].Exit[South]=8;
       Room[0].Exit[West]=2;
    
    
       strcpy(Room[1].Description, "Living Room");
       Room[1].Exit[North]=3;
       Room[1].Exit[East]=8;
       Room[1].Exit[South]=8;
       Room[1].Exit[West]=0;
    
    
       strcpy(Room[2].Description, "Garage");
       Room[2].Exit[North]=4;
       Room[2].Exit[East]=0;
       Room[2].Exit[South]=8;
       Room[2].Exit[West]=8;
    
    
       strcpy(Room[3].Description, "Dining Room");
       Room[3].Exit[North]=6;
       Room[3].Exit[East]=8;
       Room[3].Exit[South]=1;
       Room[3].Exit[West]=8;
    
    
       strcpy(Room[4].Description, "Spare Room");
       Room[4].Exit[North]=8;
       Room[4].Exit[East]=8;
       Room[4].Exit[South]=2;
       Room[4].Exit[West]=8;
    
    
       strcpy(Room[5].Description, "Kitchen");
       Room[5].Exit[North]=7;
       Room[5].Exit[East]=6;
       Room[5].Exit[South]=0;
       Room[5].Exit[West]=8;
    
    
       strcpy(Room[6].Description, "Pantry");
       Room[6].Exit[North]=8;
       Room[6].Exit[East]=8;
       Room[6].Exit[South]=3;
       Room[6].Exit[West]=4;
    
    
       strcpy(Room[7].Description, "Patio");
       Room[7].Exit[North]=8;
       Room[7].Exit[East]=8;
       Room[7].Exit[South]=4;
       Room[7].Exit[West]=8;
    
    
    }
    
    
    void DescribeRoom(unsigned int RoomNumber)
    {
       printf("Welcome to the home of James Ward's Family.\n\n");
       printf("You are currently in the %s. ", Room[RoomNumber].Description);
    }
    
    
    void DescribeExits(int RoomNumber)
    {
       unsigned int exitdir;
       unsigned int exitcount = 0;
       for (exitdir = 0; exitdir < 4; exitdir++)
       {
          if (Room[RoomNumber].Exit[exitdir] > 0) exitcount++;
       }
       if (exitcount == 1) printf("This room has %u exit: \n", exitcount);
       else printf("This room has %u exits: \n", exitcount);
       if (Room[RoomNumber].Exit[0] > 0) printf("   North: %s\n", Room[Room[RoomNumber].Exit[0]].Description);
       if (Room[RoomNumber].Exit[1] > 0) printf("   East:  %s\n", Room[Room[RoomNumber].Exit[1]].Description);
       if (Room[RoomNumber].Exit[2] > 0) printf("   South: %s\n", Room[Room[RoomNumber].Exit[2]].Description);
       if (Room[RoomNumber].Exit[3] > 0) printf("   West:  %s\n", Room[Room[RoomNumber].Exit[3]].Description);
       printf("\n");
    }
    
    
    void GetDirection(char Direction[])
    {
       printf("Type the first letter of the direction you want to go in, or type q to quit\n");
       scanf("%s", Direction);
       while(strlen(Direction) > 1)
       {
          printf("You may only type ONE letter - please try again\n");
          scanf("%s", Direction);
       }
    }
    
    
    int main(void)
    {
       char Direction[1];
       char DummyInput[1];
       char ExitName[5];
       unsigned int RoomNumber = 0;
       unsigned int NewRoomNumber, i, CheckExit, ValidExit;
       RoomSetup();
       while(1){
          system("cls"); //Clear Screen
          DescribeRoom(RoomNumber); //Describe current Room
          DescribeExits(RoomNumber); //Describe current Room Exits
          GetDirection(Direction); //Get direction to go in
          if (strcmp(Direction, "Q") == 0 || strcmp(Direction, "q") == 0) exit(0);
          ValidExit = 1;
          if (strcmp(Direction, "N") == 0 || strcmp(Direction, "n") == 0)
          {
             CheckExit = 0;
             strcpy(ExitName, "North");
          }
          else if (strcmp(Direction, "E") == 0 || strcmp(Direction, "e") == 0)
          {
             CheckExit = 1;
             strcpy(ExitName, "East");
          }
          else if (strcmp(Direction, "S") == 0 || strcmp(Direction, "s") == 0)
          {
             CheckExit = 2;
             strcpy(ExitName, "South");
          }
          else if (strcmp(Direction, "W") == 0 || strcmp(Direction, "w") == 0)
          {
             CheckExit = 3;
             strcpy(ExitName, "West");
          }
          else
          {
             ValidExit = 0;
             printf("'%s' is not a valid direction - please press any key and 'Enter' to continue\n", Direction);
             scanf("%s", DummyInput);
          }
          if (ValidExit == 1)
          {
             if (Room[RoomNumber].Exit[CheckExit] > 0) RoomNumber = Room[RoomNumber].Exit[CheckExit];
             else
             {
                printf("There is no exit in the %s - please press any key and 'Enter' to continue\n", ExitName);
                scanf("%s", DummyInput);
             }
          }
       }
       return 0;
    }
    modified code, probably wrong again but it works better than previously. compile it and run if you have time its hard to explain whats going wrong but itll make sense if you run the program yourself

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > Location Room[0];
    WTF, this was 8 in your original post!

    > Room[1].Exit[East]=8;
    > Room[1].Exit[South]=8;
    You need to change all these 8's as well.

    > if (Room[RoomNumber].Exit[exitdir] > 0)
    Since 0 is a valid array subscript, and thus a valid room, perhaps you should have

    Code:
    enum {
        NO_EXIT = -1,
        R_ENTRANCE = 0,
        R_LIVING,
        R_GARAGE,
        // and so on
    };
    Then you can meaningfully initialise your array with
    Code:
       strcpy(Room[R_ENTRANCE].Description, "Entrance Corridor");
       Room[R_ENTRANCE].Exit[North]=R_LIVING;
       Room[R_ENTRANCE].Exit[East]=R_GARAGE;
       Room[R_ENTRANCE].Exit[South]=NO_EXIT;
       Room[R_ENTRANCE].Exit[West]=NO_EXIT;
    And you specifically test for NO_EXIT when checking room exits.


    > char Direction[1];
    > char DummyInput[1];
    > char ExitName[5];
    All these arrays overflow.
    You can't fit "N" in Direction, nor "North" in ExitName without overflowing the string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Beginners sometimes overuse specialized types: unsigned, short, etc. The rule is to always use int unless there's a good reason to do otherwise. Not expecting to use negative numbers is not enough of a reason to use unsigned. Actually, it's often useful to have access to negative numbers. In your case, as Salem has suggested, you can use -1 to indicate no exit. In essense you're using the high bit as a built-in flag. It's elegant and very common (e.g., EOF is always negative and usually -1).

    You can use an enum for a cleaner definition of your directions.
    Code:
      enum { North, East, South, West };
    You could change RoomSetup to read from a file. A simple structure for the file would be:
    Code:
     5  1 -1  2  Entrance Corridor
     3 -1 -1  0  Living Room
    etc.
    With this you couldn't use Salem's idea of an enum member for the different rooms since they would no longer be hard-coded. Also, I couldn't understand all the 8's (since they seem to be out-of-range) so I changed them to -1's. You'll have to re-check your numbering.

    You need to think a little more programatically. Use variables and loops a little more. For example, this
    Code:
      if (Room[RoomNumber].Exit[0] > 0) printf("   North: %s\n", Room[Room[RoomNumber].Exit[0]].Description);
       if (Room[RoomNumber].Exit[1] > 0) printf("   East:  %s\n", Room[Room[RoomNumber].Exit[1]].Description);
       if (Room[RoomNumber].Exit[2] > 0) printf("   South: %s\n", Room[Room[RoomNumber].Exit[2]].Description);
       if (Room[RoomNumber].Exit[3] > 0) printf("   West:  %s\n", Room[Room[RoomNumber].Exit[3]].Description);
    Can be written like so:
    Code:
    // Defined globally near the top of the program
    char Direction[4] = {"North", "East", "South", "West"};
    
    // then
    int i, *x = Room[RoomNumber].Exit;
    for (i = 0; i < 4; i++, x++)
        if (*x >= 0)
            printf("   %s: %s\n", Direction[i], Room[*x].Description);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    made the recommended changes below, still doesnt work, im in over my head here just cant seem to do anything right

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <conio.h>
    
    
    #define North 0
    #define East 1
    #define South 2
    #define West 3
    
    
    typedef struct
    {
       char Description[200];
       unsigned int Exit[4];
       enum {
        NO_EXIT=-1,
        R_ENTRANCE=0,
        R_LIVING=1,
        R_GARAGE=2,
        R_DINING=3,
        R_SPARE=4,
        R_KITCHEN=5,
        R_PANTRY=6,
        R_PATIO=7,
    };
    } Location;
    
    
    Location Room[0];
    
    
    void RoomSetup(void)
    {
        strcpy(Room[R_ENTRANCE].Description, "Entrance Corridor");
        Room[R_ENTRANCE].Exit[North]=1;
        Room[R_ENTRANCE].Exit[East]=2;
        Room[R_ENTRANCE].Exit[South]=-1;
        Room[R_ENTRANCE].Exit[West]=-1;
    
    
        strcpy(Room[R_LIVING].Description, "Living Room");
        Room[R_LIVING].Exit[North]=3;
        Room[R_LIVING].Exit[East]=-1;
        Room[R_LIVING].Exit[South]=-1;
        Room[R_LIVING].Exit[West]=0;
    
    
        strcpy(Room[R_GARAGE].Description, "Garage");
        Room[R_GARAGE].Exit[North]=4;
        Room[R_GARAGE].Exit[East]=0;
        Room[R_GARAGE].Exit[South]=-1;
        Room[R_GARAGE].Exit[West]=-1;
    
    
        strcpy(Room[R_DINING].Description, "Dining Room");
        Room[R_DINING].Exit[North]=6;
        Room[R_DINING].Exit[East]=-1;
        Room[R_DINING].Exit[South]=1;
        Room[R_DINING].Exit[West]=-1;
    
    
        strcpy(Room[R_SPARE].Description, "Spare Room");
        Room[R_SPARE].Exit[North]=-1;
        Room[R_SPARE].Exit[East]=-1;
        Room[R_SPARE].Exit[South]=2;
        Room[R_SPARE].Exit[West]=-1;
    
    
        strcpy(Room[R_KITCHEN].Description, "Kitchen");
        Room[R_KITCHEN].Exit[North]=7;
        Room[R_KITCHEN].Exit[East]=6;
        Room[R_KITCHEN].Exit[South]=0;
        Room[R_KITCHEN].Exit[West]=-1;
    
    
        strcpy(Room[R_PANTRY].Description, "PANTRY");
        Room[R_PANTRY].Exit[North]=-1;
        Room[R_PANTRY].Exit[East]=-1;
        Room[R_PANTRY].Exit[South]=3;
        Room[R_PANTRY].Exit[West]=5;
    
    
        strcpy(Room[R_PATIO].Description, "PATIO");
        Room[R_PATIO].Exit[North]=-1;
        Room[R_PATIO].Exit[East]=-1;
        Room[R_PATIO].Exit[South]=5;
        Room[R_PATIO].Exit[West]=-1;
    }
    
    
    void DescribeRoom(unsigned int RoomNumber)
    {
       printf("Welcome to the home of James Ward's Family.\n\n");
       printf("You are currently in the %s. ", Room[RoomNumber].Description);
    }
    
    
    void DescribeExits(int RoomNumber)
    {
       unsigned int exitdir;
       unsigned int exitcount = 0;
       for (exitdir = 0; exitdir < 4; exitdir++)
       {
          if (Room[RoomNumber].Exit[exitdir] > 0) exitcount++;
       }
       if (exitcount == 1) printf("This room has %u exit: \n", exitcount);
       else printf("This room has %u exits: \n", exitcount);
       if (Room[RoomNumber].Exit[0] > 0) printf("   North: %s\n", Room[Room[RoomNumber].Exit[0]].Description);
       if (Room[RoomNumber].Exit[1] > 0) printf("   East:  %s\n", Room[Room[RoomNumber].Exit[1]].Description);
       if (Room[RoomNumber].Exit[2] > 0) printf("   South: %s\n", Room[Room[RoomNumber].Exit[2]].Description);
       if (Room[RoomNumber].Exit[3] > 0) printf("   West:  %s\n", Room[Room[RoomNumber].Exit[3]].Description);
       printf("\n");
    }
    
    
    void GetDirection(char Direction[])
    {
       printf("Type the first letter of the direction you want to go in, or type q to quit\n");
       scanf("%s", Direction);
       while(strlen(Direction) > 1)
       {
          printf("You may only type ONE letter - please try again\n");
          scanf("%s", Direction);
       }
    }
    
    
    int main(void)
    {
       char Direction[1];
       char DummyInput[1];
       char ExitName[5];
       unsigned int RoomNumber = 0;
       unsigned int NewRoomNumber, i, CheckExit, ValidExit;
       RoomSetup();
       while(1){
          system("cls"); //Clear Screen
          DescribeRoom(RoomNumber); //Describe current Room
          DescribeExits(RoomNumber); //Describe current Room Exits
          GetDirection(Direction); //Get direction to go in
          if (strcmp(Direction, "Q") == 0 || strcmp(Direction, "q") == 0) exit(0);
          ValidExit = 1;
          if (strcmp(Direction, "N") == 0 || strcmp(Direction, "n") == 0)
          {
             CheckExit = 0;
             strcpy(ExitName, "North");
          }
          else if (strcmp(Direction, "E") == 0 || strcmp(Direction, "e") == 0)
          {
             CheckExit = 1;
             strcpy(ExitName, "East");
          }
          else if (strcmp(Direction, "S") == 0 || strcmp(Direction, "s") == 0)
          {
             CheckExit = 2;
             strcpy(ExitName, "South");
          }
          else if (strcmp(Direction, "W") == 0 || strcmp(Direction, "w") == 0)
          {
             CheckExit = 3;
             strcpy(ExitName, "West");
          }
          else
          {
             ValidExit = 0;
             printf("'%s' is not a valid direction - please press any key and 'Enter' to continue\n", Direction);
             scanf("%s", DummyInput);
          }
          if (ValidExit == 1)
          {
             if (Room[RoomNumber].Exit[CheckExit] > 0) RoomNumber = Room[RoomNumber].Exit[CheckExit];
             else
             {
                printf("There is no exit in the %s - please press any key and 'Enter' to continue\n", ExitName);
                scanf("%s", DummyInput);
             }
          }
       }
       return 0;
    }

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Read the thread again, there's a whole bunch of things you still haven't done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Just a few of the things you've missed:
    Turn your warning levels up and pay attention to them. You can't have a zero-sized array.

    Code:
    Room[R_ENTRANCE].Exit[North]=1;
    Hmm, so north of R_ENTRANCE is room #1, and room #1 is... please wait while I look that up... oh it's R_LIVING.
    You can just assign R_LIVING instead of a one on that line.

    None of your > 0 checks make sense any more because room zero is a valid room number. You mean to compare against NO_EXIT.
    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"

  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    so like this:

    Code:
    void RoomSetup(void){
        strcpy(Room[R_ENTRANCE].Description, "Entrance Corridor");
        Room[R_ENTRANCE].Exit[North]=R_LIVING;
        Room[R_ENTRANCE].Exit[East]=R_GARAGE;
        Room[R_ENTRANCE].Exit[South]=NO_EXIT;
        Room[R_ENTRANCE].Exit[West]=NO_EXIT;
    Quote Originally Posted by iMalc View Post
    None of your > 0 checks make sense any more because room zero is a valid room number. You mean to compare against NO_EXIT.
    How would i compare against NO_EXIT rather than having >0 checks?

  14. #14
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    e.g.
    Code:
    if (Room[RoomNumber].Exit[0] != NO_EXIT) printf("   North: %s\n", Room[Room[RoomNumber].Exit[0]].Description);
    Kurt

  15. #15
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    code changed

    when i build and run this is what is displayed on startup:
    Name:  Untitled.png
Views: 217
Size:  43.0 KB

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <conio.h>
    
    
    #define North 0
    #define East 1
    #define South 2
    #define West 3
    
    
    typedef struct
    {
       char Description[200];
       unsigned int Exit[4];
       enum {
        NO_EXIT=-1,
        R_ENTRANCE=0,
        R_LIVING=1,
        R_GARAGE=2,
        R_DINING=3,
        R_SPARE=4,
        R_KITCHEN=5,
        R_PANTRY=6,
        R_PATIO=7,
    };
    } Location;
    
    
    Location Room[R_ENTRANCE];
    
    
    void RoomSetup(void)
    {
        strcpy(Room[R_ENTRANCE].Description, "Entrance Corridor");
        Room[R_ENTRANCE].Exit[North]=R_LIVING;
        Room[R_ENTRANCE].Exit[East]=R_GARAGE;
        Room[R_ENTRANCE].Exit[South]=NO_EXIT;
        Room[R_ENTRANCE].Exit[West]=NO_EXIT;
    
    
        strcpy(Room[R_LIVING].Description, "Living Room");
        Room[R_LIVING].Exit[North]=R_DINING;
        Room[R_LIVING].Exit[East]=NO_EXIT;
        Room[R_LIVING].Exit[South]=NO_EXIT;
        Room[R_LIVING].Exit[West]=R_ENTRANCE;
    
    
        strcpy(Room[R_GARAGE].Description, "Garage");
        Room[R_GARAGE].Exit[North]=R_SPARE;
        Room[R_GARAGE].Exit[East]=R_ENTRANCE;
        Room[R_GARAGE].Exit[South]=NO_EXIT;
        Room[R_GARAGE].Exit[West]=NO_EXIT;
    
    
        strcpy(Room[R_DINING].Description, "Dining Room");
        Room[R_DINING].Exit[North]=R_PANTRY;
        Room[R_DINING].Exit[East]=NO_EXIT;
        Room[R_DINING].Exit[South]=R_LIVING;
        Room[R_DINING].Exit[West]=NO_EXIT;
    
    
        strcpy(Room[R_SPARE].Description, "Spare Room");
        Room[R_SPARE].Exit[North]=NO_EXIT;
        Room[R_SPARE].Exit[East]=NO_EXIT;
        Room[R_SPARE].Exit[South]=R_GARAGE;
        Room[R_SPARE].Exit[West]=NO_EXIT;
    
    
        strcpy(Room[R_KITCHEN].Description, "Kitchen");
        Room[R_KITCHEN].Exit[North]=R_PATIO;
        Room[R_KITCHEN].Exit[East]=R_PANTRY;
        Room[R_KITCHEN].Exit[South]=R_ENTRANCE;
        Room[R_KITCHEN].Exit[West]=NO_EXIT;
    
    
        strcpy(Room[R_PANTRY].Description, "PANTRY");
        Room[R_PANTRY].Exit[North]=NO_EXIT;
        Room[R_PANTRY].Exit[East]=NO_EXIT;
        Room[R_PANTRY].Exit[South]=R_DINING;
        Room[R_PANTRY].Exit[West]=R_KITCHEN;
    
    
        strcpy(Room[R_PATIO].Description, "PATIO");
        Room[R_PATIO].Exit[North]=NO_EXIT;
        Room[R_PATIO].Exit[East]=NO_EXIT;
        Room[R_PATIO].Exit[South]=R_KITCHEN;
        Room[R_PATIO].Exit[West]=NO_EXIT;
    }
    
    
    void DescribeRoom(unsigned int RoomNumber)
    {
       printf("Welcome to the home of James Ward's Family.\n\n");
       printf("You are currently in the %s. ", Room[RoomNumber].Description);
    }
    
    
    void DescribeExits(int RoomNumber)
    {
       unsigned int exitdir;
       unsigned int exitcount = 0;
       for (exitdir = 0; exitdir < 4; exitdir++)
       {
          if (Room[RoomNumber].Exit[exitdir] > 0) exitcount++;
       }
       if (exitcount == 1) printf("This room has %u exit: \n", exitcount);
       else printf("This room has %u exits: \n", exitcount);
       if (Room[RoomNumber].Exit[0] != NO_EXIT) printf("   North: %s\n", Room[Room[RoomNumber].Exit[0]].Description);
       if (Room[RoomNumber].Exit[1] != NO_EXIT) printf("   North: %s\n", Room[Room[RoomNumber].Exit[1]].Description);
       if (Room[RoomNumber].Exit[2] != NO_EXIT) printf("   North: %s\n", Room[Room[RoomNumber].Exit[2]].Description);
       if (Room[RoomNumber].Exit[3] != NO_EXIT) printf("   North: %s\n", Room[Room[RoomNumber].Exit[3]].Description);
       printf("\n");
    }
    
    
    void GetDirection(int * Direction[])
    {
       printf("Type the first letter of the direction you want to go in, or type q to quit\n");
       scanf("%s", Direction);
       while(strlen(Direction) > 1)
       {
          printf("You may only type ONE letter - please try again\n");
          scanf("%s", Direction);
       }
    }
    int main(void)
    {
       char Direction[2];
       char DummyInput[2];
       char ExitName[10];
       unsigned int RoomNumber = 0;
       unsigned int NewRoomNumber, i, CheckExit, ValidExit;
       RoomSetup();
       while(1){
          system("cls"); //Clear Screen
          DescribeRoom(RoomNumber); //Describe current Room
          DescribeExits(RoomNumber); //Describe current Room Exits
          GetDirection(Direction); //Get direction to go in
          if (strcmp(Direction, "Q") == 0 || strcmp(Direction, "q") == 0) exit(0);
          ValidExit = 1;
          if (strcmp(Direction, "N") == 0 || strcmp(Direction, "n") == 0)
          {
             CheckExit = 0;
             strcpy(ExitName, "North");
          }
          else if (strcmp(Direction, "E") == 0 || strcmp(Direction, "e") == 0)
          {
             CheckExit = 1;
             strcpy(ExitName, "East");
          }
          else if (strcmp(Direction, "S") == 0 || strcmp(Direction, "s") == 0)
          {
             CheckExit = 2;
             strcpy(ExitName, "South");
          }
          else if (strcmp(Direction, "W") == 0 || strcmp(Direction, "w") == 0)
          {
             CheckExit = 3;
             strcpy(ExitName, "West");
          }
          else
          {
             ValidExit = 0;
             printf("'%s' is not a valid direction - please press any key and 'Enter' to continue\n", Direction);
             scanf("%s", DummyInput);
          }
          if (ValidExit == 1)
          {
             if (Room[RoomNumber].Exit[CheckExit] > 0) RoomNumber = Room[RoomNumber].Exit[CheckExit];
             else
             {
                printf("There is no exit in the %s - please press any key and 'Enter' to continue\n", ExitName);
                scanf("%s", DummyInput);
             }
          }
       }
       return 0;
    }
    Last edited by j.ward91; 04-26-2012 at 08:04 AM.

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

Similar Threads

  1. Finishing up Mastermind game program
    By Charak in forum C Programming
    Replies: 5
    Last Post: 02-17-2011, 01:49 AM
  2. Replies: 3
    Last Post: 12-03-2003, 09:15 AM
  3. SO close to finishing chat program...!
    By Nakeerb in forum C++ Programming
    Replies: 13
    Last Post: 10-26-2002, 12:24 PM
  4. Finishing up
    By Gnoober in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2002, 06:18 AM
  5. finishing up the sorting program
    By jk in forum C Programming
    Replies: 2
    Last Post: 03-19-2002, 06:43 PM

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