Like Tree1Likes

Problem! C Programming Begginer Soooo Confused :(

This is a discussion on Problem! C Programming Begginer Soooo Confused :( within the C Programming forums, part of the General Programming Boards category; ok thanks now im a little more confused. if i dont need switch/case what would be better to use? is ...

  1. #16
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    ok thanks now im a little more confused. if i dont need switch/case what would be better to use? is it anything closer to what i was thinking before with the if statements or should i try stick with the switch/case?
    Code:
    //GET DIRECTION FUNCTION
    
    int getDirection(int room){
    
    printf("\nType the letter of the direction you wish to go in \nOr type q to quit");
    	
    	//To HERE*
    	
    	user presses "n"
    		computer looks at current room // scanf maybe???
    		if exit[North]> 0
    			goto corresponding room
    			else if exit north = 0
    				printf("\ncannot go in this diresction, please select a different direction")
    		
    		if completely wrong key pressed (so not n,e,s,w or q)
    			printf("\ninvalid key, please only use n,e,s,w or q")
    			//LOOP FROM HERE*
    }
    again i know this is not proper code language, i am jus quite confused. any help would be great

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Maybe a switch/case makes it easier since we are dealing with chars for the direction...

    pseudo code
    Code:
    getDirection( int room)
    
      user enters direction_char
      switch(direction_char) {
          case 'n':
              direction_int = 0;
              break;
          case 'e':
              direction_int = 1;
              break;
               .
               .
               .
          case 'q':
               ...
          default:
              break;
      }
      NewRoomNumber = Room[room].Exit[direction_int] //    I gave you this before
       return NewRoomNumber;

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First of all... you would likely benefit a lot from simplifying your structure... An array in a structure is not easy coding...

    Code:
    typedef struct
    {  char Description[200]; 
       unsigned int Exit[4]; } 
       Location;
    can very easily be simplified to this...

    Code:
    typedef struct
      {  char Description[200]; 
          int East;
          int West;
          int North;
          int South;  }
          Location;
    Now instead of having to write Room[room].exit[direction] == you can write Room[room].East == which would be a LOT less confusing.

  4. #19
    Registered User
    Join Date
    Apr 2011
    Posts
    7

    re revised post

    I think i am almost there i have a few glitches when i run it. it will not accept the commands i input im not too sure what it is tho, any help would be fantastic. thanks so much for all your help. i feel like i have learnt a lot this is all my code:
    Code:
    // Required System Files
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    // Global Definitions
    #define NORTH    0
    #define EAST     1
    #define SOUTH    2
    #define WEST     3
    
    // Global Structure Type Definition
    typedef struct {           // Define new Structure Type
        char description[200]; // Use upto 200 characters for description string
        int Exit[4];  // Use 4 possible different values for Exit array
    } Location;                // Call this structure type: Location
    
    // Global Location Type Variable Definition
    Location Room[14]; // Create 13 Location type Global structures called Room’s
    
    // function prototypes
    void setupRooms(void);
    void describeRoom(int roomNumber);
    void describeExits(int roomNumber);
    int getDirection(int currentRoom);
    
    //SETUP ROOM FUNCTION
    void setupRooms() {
        // MY ROOM 1 DEFINITION
        strcpy(Room[1].description, "Entrance");
        Room[1].Exit[NORTH] = 2; // Going north takes you to Room 2
        Room[1].Exit[EAST]  = 0; // Use zero to indicate no exit East
        Room[1].Exit[SOUTH] = 0;
        Room[1].Exit[WEST]  = 0;
    
        // MY ROOM 2 DEFINITION
        strcpy(Room[2].description, "Lower South Corridor");
        Room[2].Exit[NORTH] = 4;
        Room[2].Exit[EAST]  = 3;
        Room[2].Exit[SOUTH] = 1;
        Room[2].Exit[WEST]  = 0;
    
            // MY ROOM 3 DEFINITION
            strcpy(Room[3].description, "Living Room");
        Room[3].Exit[NORTH] = 0;
        Room[3].Exit[EAST]  = 0;
        Room[3].Exit[SOUTH] = 0;
        Room[3].Exit[WEST]  = 2;
    
            // MY ROOM 4 DEFINITION
            strcpy(Room[4].description, "Lower North Corridor");
        Room[4].Exit[NORTH] = 6;
        Room[4].Exit[EAST]  = 5;
        Room[4].Exit[SOUTH] = 2;
        Room[4].Exit[WEST]  = 0;
    
            // MY ROOM 5 DEFINITION
            strcpy(Room[5].description, "Stairs");
        Room[5].Exit[NORTH] = 0;
        Room[5].Exit[EAST]  = 8;
        Room[5].Exit[SOUTH] = 0;
        Room[5].Exit[WEST]  = 4;
    
            // MY ROOM 6 DEFINITION
            strcpy(Room[6].description, "Kitchen");
        Room[6].Exit[NORTH] = 7;
        Room[6].Exit[EAST]  = 0;
        Room[6].Exit[SOUTH] = 4;
        Room[6].Exit[WEST]  = 0;
    
            // MY ROOM 7 DEFINITION
            strcpy(Room[7].description, "Garden");
        Room[7].Exit[NORTH] = 0;
        Room[7].Exit[EAST]  = 0;
        Room[7].Exit[SOUTH] = 6;
        Room[7].Exit[WEST]  = 0;
    
            // MY ROOM 8 DEFINITION
            strcpy(Room[8].description, "Upper North corridor");
        Room[8].Exit[NORTH] = 9;
        Room[8].Exit[EAST]  = 0;
        Room[8].Exit[SOUTH] = 10;
        Room[8].Exit[WEST]  = 5;
    
            // MY ROOM 9 DEFINITION
            strcpy(Room[9].description, "Main Bedroom");
        Room[9].Exit[NORTH] = 0;
        Room[9].Exit[EAST]  = 0;
        Room[9].Exit[SOUTH] = 8;
        Room[9].Exit[WEST]  = 0;
    
            // MY ROOM 7 DEFINITION
            strcpy(Room[10].description, "Upper South Corridor");
        Room[10].Exit[NORTH] = 8;
        Room[10].Exit[EAST]  = 11;
        Room[10].Exit[SOUTH] = 12;
        Room[10].Exit[WEST]  = 13;
    
            // MY ROOM 11 DEFINITION
            strcpy(Room[11].description, "Bathroom");
        Room[11].Exit[NORTH] = 0;
        Room[11].Exit[EAST]  = 0;
        Room[11].Exit[SOUTH] = 0;
        Room[11].Exit[WEST]  = 10;
    
    
            // MY ROOM 12 DEFINITION
            strcpy(Room[12].description, "Guest Room");
        Room[12].Exit[NORTH] = 10;
        Room[12].Exit[EAST]  = 0;
        Room[12].Exit[SOUTH] = 0;
        Room[12].Exit[WEST]  = 0;
    
            // MY ROOM 13 DEFINITION
            strcpy(Room[13].description, "Kids Bedroom");
        Room[13].Exit[NORTH] = 0;
        Room[13].Exit[EAST]  = 10;
        Room[13].Exit[SOUTH] = 0;
        Room[13].Exit[WEST]  = 0;
    }
    
    //DESCRIBE ROOM FUNCTION
    
    void describeRoom (int roomNumber) {
        printf("You are in: %s\n",  Room[roomNumber].description);
    }
    
    //DESCRIBE EXITS FUNCTION
    
    void describeExits(int roomNumber) {
    
    printf("\nExits available are to the");
    
                    if ( Room[roomNumber].Exit[NORTH] > 0 )
                           {
                            printf("\nNorth");
                           }
    
                    if ( Room[roomNumber].Exit[EAST] > 0 )
                           {
                            printf("\nEast");
                            }
    
                    if ( Room[roomNumber].Exit[SOUTH] > 0 )
                    {
                            printf("\nSouth");
                            }
    
                    if ( Room[roomNumber].Exit[WEST] > 0 )
                     {
                            printf("\nWest");
                     }
    }
    
    ////GET DIRECTION FUNCTION
    
    int getDirection(int currentRoom){
    
    printf("\nType the letter of the direction you wish to go in \nOr type q to quit");
    
    char direction='q';
    
    scanf("%c", &direction);
    
    
            switch(direction){
                    case 'n':
                    case 'N':
    
                            if(Room[currentRoom].Exit[NORTH] > 0)
                            {
    
                    getchar();
    
                             return Room[currentRoom].Exit[NORTH];
    
                            }
                            break;
                     case 's':
                    case 'S':
                            if(Room[currentRoom].Exit[SOUTH] > 0)
                            {
                    getchar();
                             return Room[currentRoom].Exit[SOUTH];
                            }
    
                            break;
                    case 'e':
                    case 'E':
                            if(Room[currentRoom].Exit[EAST] > 0)
                            {
                    getchar();
                             return Room[currentRoom].Exit[EAST];
    
                            }
                            break;
                     case 'w':
                    case 'W':
                            if(Room[currentRoom].Exit[WEST] > 0)
                            {
                    getchar();
                             return Room[currentRoom].Exit[WEST];
                            }
    
                            break;
                    case 'q':
                    case 'Q':
                            getchar();
                            return -1;
                            break;
    
                    default:
                            printf("\nInvalid key, please only use n,e,s,w or q");
                            break;
            }
    
            getchar();
            return -2;
    }
    
    
    // MAIN FUNCTION
    int main(void) {
        int roomNumber = 1; // Setup starting room number as 1
        int direction = 0;
    
        setupRooms(); // Setup Room structures
    
        while (1) {
    
            system("cls");                          // Clear Screen
            if (direction == -2)
            {
                          printf("\nYou cannot go that way");
                          }
            describeRoom(roomNumber);               // Describe current Room
    
            describeExits(roomNumber);              // Describe current Room Exits
    
            direction = getDirection(roomNumber);   // Get direction to go in
    
    
            if (direction == -1 ) {
                break;                            // Exit prog if error is returned
            }
    
            if (direction >= 0) {
                roomNumber = direction;             // Set current Room number
    
            }
    
    
        }
    
        return 0;
    }
    thanks again for all the help you have given
    Last edited by sketch8; 04-26-2011 at 05:29 PM. Reason: made a mistake in the code

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    It seems to work for me (though there are some linefeed issues that you could clean up)

    not accept the commands i input
    Not sure what you mean. Maybe: scanf requires the user hit "enter" after the character. You could use get_char() if you wanted the program to respond immediately to a keypress.

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

Similar Threads

  1. Confused about this C problem
    By alexbcg in forum C Programming
    Replies: 7
    Last Post: 11-23-2010, 01:41 PM
  2. Begginer Problem: Extracting words from sentence
    By barlas in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 03:17 PM
  3. Yahoo! mail is bieng soooo annoying !!!!
    By Brain Cell in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 11-08-2004, 02:23 PM
  4. Do { While Loop Problem - Note: Begginer Programmer
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2002, 10:28 AM
  5. Struct Problem (Begginer)
    By Venom in forum C Programming
    Replies: 1
    Last Post: 03-08-2002, 05:18 AM

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