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; when i build and run this is what is displayed on startup: I take this as a question. The answer: ...

  1. #16
    ZuK
    ZuK is offline
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
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