Thread: help finishing this program

  1. #16
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    when i build and run this is what is displayed on startup:
    I take this as a question.
    The answer:
    Code:
    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");
    }
    You print North for each exit.
    When counting the exits you should compare for != NO_EXIT as well

    Kurt

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (Room[RoomNumber].Exit[0] != NO_EXIT) printf(" North: %s\n", Room[Room[RoomNumber].Exit[0]]
    What about Room[Room[Room[ or Room[Room[Room[Room[Room[Room[ ?

    There are still a lot of 0 to 3 all over the place.
    You've learnt about enum, so maybe
    Code:
    enum {
        North,
        East,
        South,
        West,
        MAX_DIRN
    };
    Then
    Code:
    const char *dirns[] = { "North", "East", "South", "West" };
    And finally (phew)
    Code:
    for ( dirn = 0 ; dirn < MAX_DIRN ; dirn++ ) {
        if ( Room[RoomNumber].Exit[dirn] != NO_EXIT) 
            printf("   %s: %s\n", dirns[dirn], Room[RoomNumber].Description );
    }
    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.

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, 02:49 AM
  2. Replies: 3
    Last Post: 12-03-2003, 10: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, 07:43 PM