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; i am trying to create a basic text based game which will simply navigate between rooms when u press the ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    7

    Problem! C Programming Begginer Soooo Confused :(

    i am trying to create a basic text based game which will simply navigate between rooms when u press the corresponding letter. eg. n = north.
    i have a basic code here which i have been given to work with and i keep getting errors where i am trying to define the rooms, in lines 21 - 32. i jus want two rooms for now that i can move back and forth between, the later version should have 13 rooms but once i know how to do the first two i assume the others will be easy to add in. if you have any ideas how to help it would be greatly appreciated. i am new at c and have a very limited knowledge, so i do not understand much which does not help. sorry. here is the code i have so far with annotations of what i am trying to do:

    Code:
    //Required System Files
    #include <stdio.h>
    #include <stdlib.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
    unsigned int Exit[4]; //Use 4 possible different values for Exit array
    }Location; //Call this structure type: Location
    
    //Global Location Type Variable Definition
    Location Room[13]; //Create 13 Location type Global structures called Room’s
    
    //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;.
    
    //Main Function
    void main(){
    unsigned int RoomNumber = 1; //Setup starting room number as 1
    unsigned int Direction;
    SetupRooms(); //Setup Room structures
    while(1){
    
    system("cls"); //Clear Screen
    DescribeRoom(RoomNumber); //Describe current Room
    DescribeExits(RoomNumber); //Describe current Room Exits
    Direction = GetDirection(RoomNumber); //Get direction to go in
    if(Direction == 99) exit(0); //Exit prog if 99 is returned
    if(Direction > 0) RoomNumber = Direction;//Set current Room number
    }
    }
    i think i need more functions and stuff but i do not know how to do it, or what really to do. in short i am baffled. like i said before, any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Congrats on using CODE tags. You need to clean up the indentation a little.

    Code:
    #define North= 0
    #define East= 1
    #define South= 2
    #define West= 3
    Get rid of the equal signs

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    This stuff needs to be inside a function ( I suggest an initialization function you call from within main() ) edit: OK, I see you seem to want a SetupRooms() function, but didn't create it...

    Code:
    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;.

    Take baby steps - get one small thing to work, then move on.

    Please provide compilable code next time.
    Can you debug?
    Last edited by mike65535; 04-20-2011 at 10:04 AM.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    Quote Originally Posted by mike65535 View Post
    This stuff needs to be inside a function ( I suggest an initialization function you call from within main() ) edit: OK, I see you seem to want a SetupRooms() function, but didn't create it...

    Code:
    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;.

    Take baby steps - get one small thing to work, then move on.

    Please provide compilable code next time.
    Can you debug?
    thanks soo much for your help. i have edited the code so it is now compilable, granted with quite a bit (loads) of help.

    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
        unsigned int Exit[4];  // Use 4 possible different values for Exit array
    } Location;                // Call this structure type: Location
     
    // Global Location Type Variable Definition
    Location Room[13]; // Create 13 Location type Global structures called Room’s
     
    // function prototypes
    void setupRooms(void);
    void describeRoom(int room);
    void describeExits(int room);
    int getDirection(int room);
     
    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;
    	
    }
     
    // Main Function
    int main(void) {
        unsigned int roomNumber = 1; // Setup starting room number as 1
        unsigned int direction;
     
        setupRooms(); // Setup Room structures
     
        while (1) {
            system("cls");                          // Clear Screen
            describeRoom(roomNumber);               // Describe current Room
    			
            describeExits(roomNumber);              // Describe current Room Exits
    			
            direction = getDirection(roomNumber);   // Get direction to go in
     
            if (direction == 99) {
                exit(0);                            // Exit prog if 99 is returned
            }
     
            if (direction > 0) {
                roomNumber = direction;             // Set current Room number
            }
        }
        return 0;
    }
    i have added in the definitions of the other rooms now aswell. what i am stuck on now is being able to actually run the code as it is missing some functions. i am still quite confused on how to do this i think i need some printf or scanf functions in the main by the describeRooms describeExits bit but i am not too sure what to put or how to do this. if you could give any help this would again be grealy appreciated. if not dont worry you have already helped so much, which i am very grateful for
    Last edited by sketch8; 04-24-2011 at 04:50 PM.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Take it step by step.

    For example, write your "describeRoom" function and make a temporary test main() to call describeRoom with several values to be sure it works.

    describeRoom() needs to do a simple print - like this

    Code:
    void describeRoom (int room_num) {
        printf("You are in: %s\n",  Room[room_num].description);
    }
    ( I don't understand what describeExits is intended to do. Isn't the user supposed to "guess" the valid exit(s)? )
    Last edited by mike65535; 04-25-2011 at 08:20 AM.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    thanks

    when your in the room, it shopuld say something like "exits are available to your" and let you know where, this is what i have attempted

    Code:
    //DESCRIBE EXITS FUNCTION
    
    void describeExits() {
    
    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");
    			
    }
    there are something like 16 errors am i on the right tracks with this code or is it completely wrong? thanks soo much for your help

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    You need parens around the if parameters

    Code:
    		if ( Room[roomNumber].Exit[SOUTH] > 0 )
    			printf("\nSouth");

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Quote Originally Posted by sketch8 View Post
    right tracks
    Yes, I suppose with what I can see.

    You also need to pass in the room number.
    Code:
    void describeExits(int roomNumber) {

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    thanks for your help

    i am stuck again now on trying to get the getDirection function. this part i am striggling with as i have very little coding knowledge and am not too sure how to code it any help would again be extremely grateful or just a prod in the right direction here is what i am trying to do:

    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*
    }
    i understand a lot of this is not programming language, i am trying to show how i want it to work with what little programming knowledge i have. the example above is jus for the press of "n", im guessign once i have this "e", "s", "w" and "q" should all be fairly similar and possible for me to work out.

    thanks so much for your help so far! if you cannot help anymore it is ok the help that you have gave has been invaluable and has been greatly appreciated

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to make a real effort to learn the language... Moaning about not knowing it is NOT going to solve your problem. Get out the books, read the pages, do the exercises... you'll be glad you did.

    For your immediate problem, look up scanf(), getchar() and switch() for some good hints on how to deal with it.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    thanks for pointing me in the right direction. i have been trying to get my head around scanf(), getchar() and switch(). so for my problem of trying to navigate would this possibly work:

    Code:
    int getDirection(int room){
    
    printf("\nType the letter of the direction you wish to go in \nOr type q to quit");
    
    scanf("%d", &room)
    	switch(roomNumber){
    		case exit[North]<0:
    			goto Room[roomNumber]
    			break;
    		case exit[North]=0:
    			printf("\ncannot go in this diresction, please select a different direction")
    			break;
    		default:
    			printf("\nInvalid key, please only use n,e,s,w or q")
    			break;
    	}
    	getchar();
    }
    i know there are lots of errors i have been trying to solve them but im quite stuck. is this roughly what the code should look like? am i on the right tracks now or still completely lost. thanks for your time and any help again would be extremely grateful

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, first off you can't do math in a case statment... You should be getting a compiler error telling you that "case must be const" or something similar...

    You're half way there.

    You have a user input to decide which exit... now all you need to do is figure out what you need to do with each choice the user can make (i.e. switch on their choice not your room array)...

    Also is there some reason you are changing a variable you pass into a function? It would make more sense to declare the user's input variable locally and, if needed, return a roomnumber to tell your main code where the user landed.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    What you've called room in getDirection's parameter list, should really be roomNumber. ( Hopefully you understand why)

    Then you are going to want to read in one char for direction (could use getchar or scanf) and (if you insist on using a switch/case) then test (switch) on the value of that char - not on the value of roomNumber as you've done.

    So

    Code:
    switch(direction)
    the cases will be

    Code:
    case 'n':
    CommonTater likes this.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    You really don't need a switch/case since

    Code:
    NewRoomNumber = Room[CurrentRoomNumber].Exit[direction]

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mike65535 View Post
    You really don't need a switch/case since

    Code:
    NewRoomNumber = Room[CurrentRoomNumber].Exit[direction]
    Doooh... now why didn't I see that?

Page 1 of 2 12 LastLast
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, 02:41 PM
  2. Begginer Problem: Extracting words from sentence
    By barlas in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 04: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, 03:23 PM
  4. Do { While Loop Problem - Note: Begginer Programmer
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2002, 11:28 AM
  5. Struct Problem (Begginer)
    By Venom in forum C Programming
    Replies: 1
    Last Post: 03-08-2002, 06:18 AM

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