Thread: Slight Confusion

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

    Slight Confusion

    So first off, i'm new to this. Very Very new.

    I'm currently writing a text based game in Ansi C (some of you may of noticed a few of us doing this and asking around here for guidance).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define North=0
    #define East=1
    #define South=2
    #define West=3
    typedef struct{
      char RoomName[30];
      char Description[500];
      unsigned int Exit[4];
    }Location;
    Location Room[7];
    void SetupRooms()
    {
    strcpy(Room[1].RoomName, "AirLock");
    strcpy(Room[1].Description,"As you enter the facility you find your comms channels have scrambled. \nYou appear to be in some kind of air lock. \nIn front of you the light turns green and you can hear bolts shifting.");
    Room[1].Exit[North]=2;
    Room[1].Exit[East]=0;
    Room[1].Exit[South]=50;
    Room[1].Exit[West]=0;
    strcpy(Room[2].RoomName, "Main Canteen");
    strcpy(Room[2].Description,"\"Arrrgghhhhhhh\" A muffled scream dies out somewhere up ahead as you \nstep into a wide berthed room full of regimented tables and chairs, \nto the left is a food station. Looks like your in the canteen.");
    Room[2].Exit[North]=3;
    Room[2].Exit[East]=4;
    Room[2].Exit[South]=1;
    Room[2].Exit[West]=5;
    strcpy(Room[3].RoomName, "Stasis Chamber");
    strcpy(Room[3].Description,"After a brief scan from the remaining functional automatic \nsystems you enter a completely white room with a large bank of computer systems next to a row of malfunctioning cryogenic bays. \nBehind you comes those sounds from earlier, very very close. Its found you.\n No time for lockdown, There is only one other door aside from where you came in. To the escape pods!");
    Room[3].Exit[North]=0;
    Room[3].Exit[East]=0;
    Room[3].Exit[South]=0;
    Room[3].Exit[West]=7;
    strcpy(Room[4].RoomName, "Decontamination Chamber");
    strcpy(Room[4].Description,"Tentatively you follow the big warning signs and step into a room with plastic shawls on each side with shower heads poking through. Its impossible to tell how big the room actually is beyond this and shadows dance menacingly behind every surface.");
    Room[4].Exit[North]=0;
    Room[4].Exit[East]=0;
    Room[4].Exit[South]=0;
    Room[4].Exit[West]=2;
    strcpy(Room[5].RoomName, "Containment Facility");
    strcpy(Room[5].Description,"The doors are heavy, but warped and twisted. You crouch and make your way through a gap whilst trying not to question the damp surface. Once in you can see that the containment room itself is severly damaged, chemical burns splatter the walls and a metallic odour flips your stomach. Suddenly a wet \"Thud\" and snarl sounds from the corridor in front of you leading to the cages. Everything in your body wants to turn back.");
    Room[5].Exit[North]=0;
    Room[5].Exit[East]=2;
    Room[5].Exit[South]=0;
    Room[5].Exit[West]=6;
    strcpy(Room[6].RoomName, "Cages");
    strcpy(Room[6].Description,"Against everything, you carry on towards the sound. This is the place where they must have been keeping whatever it is. What you've been sent to secure. The air is heavy here and there are definite sounds of life from the end cage. Whatever it is, its in there. Now you must isolate the facility from the central control unit in the crews stasis chamber and leave as quickly as you can.");
    Room[6].Exit[North]=0;
    Room[6].Exit[East]=5;
    Room[6].Exit[South]=0;
    Room[6].Exit[West]=0;
    strcpy(Room[7].RoomName, "Escape Pods");
    strcpy(Room[7].Description,"Houses the household computer and bookshelves filled to the brim. Rob is in the corner fapping. Its too late, the creature has found you...its Paedobear. The next scene has been censored.");
    Room[7].Exit[North]=50;
    Room[7].Exit[East]=0;
    Room[7].Exit[South]=0;
    Room[7].Exit[West]=0;
    }
    void DescribeRoom(unsigned int i)
    {
     printf("You are sent to a home facility for testing of new biological sciences after it stopped responding to communications. \nYou are to secure that the stored subjects are still within the facility and initilise lockdown.\n \n");
     printf("%15d- Maps indicate that your are in the ");
     printf(Room[i].RoomName);
     printf(" -0\n\n\n\n");
     printf(Room[i].Description);
     printf("\n\n");
    }
    void DescribeExits(unsigned int i)
    {
     printf("There are doors to your;\n");
     if(Room[i].Exit[North]!=0) printf("North\n");
     if(Room[i].Exit[East]!=0) printf("East\n");
     if(Room[i].Exit[South]!=0) printf("South\n");
     if(Room[i].Exit[West]!=0) printf("West\n");
     printf("\nInput the first letter of your desired direction to move. \nPress q to quit the system.\n");
    }
    int GetDirection(unsigned int i)
    {
     char test;
     unsigned int NewDirection=0;
     while(1)
     {
      test=getch();
      if(test=='n') NewDirection=Room[i].Exit[North];
      if(test=='e') NewDirection=Room[i].Exit[East];
      if(test=='s') NewDirection=Room[i].Exit[South];
      if(test=='w') NewDirection=Room[i].Exit[West];
      if(test=='q') NewDirection=50;
      
      if(NewDirection!=0) return(NewDirection);
      printf("Theres no way you can go there.\n");
     }
    }
     
    void main()
    {
     unsigned int RoomNumber = 1; 
     unsigned int Direction;
     SetupRooms();
      while(1)
      {
       system("cls");
       DescribeRoom(RoomNumber);
       DescribeExits(RoomNumber);
       Direction = GetDirection(RoomNumber);
       if(Direction==50)exit(0);
       if(Direction>0)RoomNumber= Direction;
      }
    }
    void print_house(unsigned int RoomNumber, unsigned int Direction){ // set up Room numbers and Directions
     char Room_x[RoomNumbers];
     unsigned int i;
     for ( i=0; i<RoomNumbers; ++i )
      Room_x[i] = ' ';
     Room_x[RoomNumber] = 'X';
     printf("___________________________________________________________\n");
     printf("|                    _____________________                |\n");
     printf("|                    |                   |                |\n");
     printf("|        __________  |                   |                |\n");
     printf("|       /          | |                   |                |\n");
     printf("|      /           |_|     Stasis        |                |\n");
     printf("|     / Escape             Chamber       |                |\n", Room_x[7]);
     printf("|     \  Pod  %c    _                    |                |\n");
     printf("|      \           | |        %c         |                |\n", Room_x[3]);
     printf("|       \__________| |                   |                |\n");
     printf("|                    |                   |                |\n");
     printf("|                    |________   ________|                |\n");
     printf("|                     _______|   |________                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|________  ___________|                  | _______________|\n");
     printf("|        ||           |                  ||               |\n");
     printf("|  Cages ||Containment|                  ||Decontamination|\n");
     printf("|   %c      Facility                           Chamber    |\n", Room_x[6]);
     printf("|              %c                                         |\n", Room_x[5]);
     printf("|        ||           |     Canteen      ||       %c      |\n", Room_x[4]);
     printf("|________||___________|                  ||_______________|\n");
     printf("|                     |        %c        |                |\n", Room_x[2]);
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |_____     ________|                |\n");
     printf("|                        |         |                      |\n");
     printf("|________________________| Airlock |______________________|\n");
     printf("                         |   %c    |                       \n", Room_x[1]);
     printf("                         |___   ___|                       \n");
    }

    I understand my program may be very confused and inefficient, but for now the main issue i am trying to tacke is the text map seen near the bottom. At the moment i have the errors:

    Undefined Symbol RoomNumbers in function 'print_house'.
    And
    Non Scalar or negative array index in function 'print_house'

    both on line 111.

    I understand that i've not properly defined RoomNumbers, but everytime i put a char in for it i get 'internal error'

    Any advice would be greatly appreciated. On the problem at hand first, and then i can start to worry about other things.

    Thanks
    Last edited by L.Firth; 04-28-2012 at 09:04 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your compiler must have given you a lot more diagnostics

    Code:
    #define North=0
    #define East=1
    #define South=2
    #define West=3
    will not work
    use
    Code:
    #define North 0
    #define East 1
    #define South 2
    #define West 3
    As you'r e using globals anyway you could just as well write
    Code:
    #define RoomNumbers 7
    typedef struct{
      char RoomName[30];
      char Description[500];
      unsigned int Exit[4];
    }Location;
    Location Room[RoomNumbers];
    haven't looked for any other errors.
    Kurt
    Last edited by ZuK; 04-28-2012 at 09:16 AM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Ok. The undefined symbol error is obvious (I hope).

    This one:
    Non Scalar or negative array index in function 'print_house'
    Are you trying to create an array of RoomNumbers chars? And RoomNumbers is what.. a variable?

    You're not allowed to use variables to declare the size of an array in C90. In C99 you can, and I believe gcc supports variable length arrays too.

    Code:
    #define LEN 10
    
    
    char arr1[10];   //ok
    char arr2[LEN]; //ok
    int foo = 10;
    char arr3[foo]; // Illegal, compiler error.
    I'm not sure I got what you meant by "put a char in it" -- can you post what you've tried if you're still stuck?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Now I understand this Room_x thingy. You want to print it in the diagram.
    you propably want
    Code:
     for ( i=0; i<RoomNumbers; ++i )
      Room_x[i] = '0' + i;

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Kurt - Thankyou for your help, however those sections you pointed out do indeed work as the program functions fine with no errors as it stands aside from these issues with the map. I appreciate help with the messy code elsewhere but first i want to get something with all parts functioning before cleaning it up.

    Smokeyangel - The undefined symbol error is obvious, however whenever i attempt to define it i just get 'internal error' from my compiler.

    I believe the scalar error may be related to the undefined symbol as it seems i have confused soemthing during this part of the code:
    Code:
    void print_house(unsigned int RoomNumber, unsigned int Direction){ // set up Room numbers and Directions
     char Room_x[RoomNumbers];
     unsigned int i;
     for ( i=0; i<RoomNumbers; ++i )
      Room_x[i] = ' ';
     Room_x[RoomNumber] = 'X';
    


    Thankyou for your patience with me whilst i get to grips with this.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Can you show me where and how you're trying to define it?

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    sorry
    Code:
    #define North=0
    Room[1].Exit[North]=2;
    cannot work
    the preprocessor would translate this to
    Code:
    Room[1].Exit[North=0]=2;
    and then the compiler must complain about a unknown symbol North
    Kurt
    EDIT: I could imagine that this is the reason for an internal error.
    Last edited by ZuK; 04-28-2012 at 09:32 AM.

  8. #8
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    you'll need string.h lib.
    Code:
    	printf("%15d- Maps indicate that your are in the ");
    you used %d with intention of printing out integer, but there is no second argument.

    Code:
    void print_house(unsigned int RoomNumber, unsigned int Direction){ // set up Room numbers and Directions
     char Room_x[RoomNumbers];
    Can't have variable name mismatch like that.
    Code:
    Location Room[7];
    
    Room[7].Exit[North]=50;
    Room[7].Exit[East]=0;
    Room[7].Exit[South]=0;
    Room[7].Exit[West]=0;
    You exceed the length of your array. C's array starts from 0.

    Code:
     printf("|     / Escape             Chamber       |                |\n", Room_x[7]);
     printf("|     \  Pod  %c    _                    |                |\n");
    "%c" is put in the wrong printf. Make sure "%c" apperas in the same printf as your variable.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Kurt - It seems to function with no complaints without the map section even with the code how it stands, however i have now updated it to:
    Code:
    #define North 0
    #define East 1
    #define South 2
    #define West 3
    Smokeyangel - I have tried to define it with:
    Code:
    void print_house(unsigned int RoomNumber, unsigned int Direction, unsigned int RoomNumbers){ // set up Room numbers and Directions
     char Room_x[RoomNumbers];
     unsigned int i;
     for ( i=0; i<RoomNumbers; ++i )
      Room_x[i] = ' ';
     Room_x[RoomNumber] = 'X';
    but i can tell this isnt right.

    Maybe it would help if i further explained. What i want it to do. Essentially as the user moves around between these rooms i want the map to update the %c under each room name being X whilst when they are not in the room it should be just a space.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    In your diagram at the bottom, you have to replace every \ with \\.
    Since \ is special, to get an actual \ to print, you need two.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    'print_house' return void; so get rid of that 'return 0' at the end. Main should return an integer, so replace that 'void' in front of main, and put 'return 0' at the end of it.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  12. #12
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    so you want
    Code:
    for ( i=0; i<RoomNumbers; ++i ) {
        if ( i == RoomNumber)
            Room_x[i] = 'X';
        else
            Room_x[i] = ' ';
    }

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is 'RoomNumber'? You have 'RoomNumbers, but I don't see 'RoomNumber':
    Code:
    Room_x[RoomNumber] = 'X';
    If you switch that out to be i, at that point you will be out of range of what your array is allowed to try to access.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Ok so currently i have:
    Code:
    void print_house(unsigned int RoomNumber, unsigned int Direction){ // set up Room numbers and Directions
     char Room_x[RoomNumber];
     unsigned int i;
     for ( i=0; i<RoomNumber; ++i ) {
        if ( i == RoomNumber)
            Room_x[i] = 'X';
        else
            Room_x[i] = ' ';
    Above my map. Thankyou to oogabooga and nimitz hunter for helping me clean up the map a little - now looks like this:
    Code:
    printf("___________________________________________________________\n");
     printf("|                    _____________________                |\n");
     printf("|                    |                   |                |\n");
     printf("|        __________  |                   |                |\n");
     printf("|       /          | |                   |                |\n");
     printf("|      /           |_|     Stasis        |                |\n");
     printf("|     / Escape             Chamber       |                |\n");
     printf("|    \\  Pod  %c    _                    |                |\n", Room_x[7]);
     printf("|     \\           | |        %c         |                |\n", Room_x[3]);
     printf("|      \\__________| |                   |                |\n");
     printf("|                    |                   |                |\n");
     printf("|                    |________   ________|                |\n");
     printf("|                     _______|   |________                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|________  ___________|                  | _______________|\n");
     printf("|        ||           |                  ||               |\n");
     printf("|  Cages ||Containment|                  ||Decontamination|\n");
     printf("|   %c      Facility                           Chamber    |\n", Room_x[6]);
     printf("|              %c                                         |\n", Room_x[5]);
     printf("|        ||           |     Canteen      ||       %c      |\n", Room_x[4]);
     printf("|________||___________|                  ||_______________|\n");
     printf("|                     |        %c        |                |\n", Room_x[2]);
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |                  |                |\n");
     printf("|                     |_____     ________|                |\n");
     printf("|                        |         |                      |\n");
     printf("|________________________| Airlock |______________________|\n");
     printf("                         |   %c    |                       \n", Room_x[1]);
     printf("                         |___   ___|                       \n");
    }

  15. #15
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by quzah View Post
    What is 'RoomNumber'?
    If the op has followed my advice RoomNumbers should be a #define for 7
    But we have to see the updated code.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slight dilemma...
    By Junior89 in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2005, 06:26 AM
  2. slight problem
    By duvernais28 in forum C Programming
    Replies: 4
    Last Post: 02-03-2005, 11:03 AM
  3. slight problem on win app tut
    By JustPlay4Fun in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2005, 11:42 AM
  4. some slight help..
    By ahming in forum C Programming
    Replies: 9
    Last Post: 09-05-2004, 08:02 AM
  5. Slight problem...
    By Fordy in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 08-10-2002, 01:32 PM