Like Tree3Likes

Slight Confusion

This is a discussion on Slight Confusion within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #define North 0 #define East 1 #define South 2 #define West 3 typedef struct{ char ...

  1. #31
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    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");
       print_house(RoomNumber);
       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
    #define RoomNumbers 7
     char Room_x[RoomNumbers];
     unsigned int i;
     for ( i=0; i<RoomNumbers; ++i ) {
        if ( i == RoomNumber)
            Room_x[i] = 'X';
        else
            Room_x[i] = ' ';
    }
     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");
    }
    -Smokeyangel, this is my current code. I think i may of been quite silly in not calling it, thankyou very much with your time on this its very very much appreciated.

    I can tell we are almost there. With the code above i have the error now of:
    Type missmatch in redeclaration of print_house
    On line: 110

  2. #32
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Code:
    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;
    }
    C array starts at '0', for array size 7, the last element MUST end at 6
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #33
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    pp.c:86:3: warning: implicit declaration of function 'getch' [-Wimplicit-function-declaration]
    because you didn't include the lib for it. I belive getch need conio.h(may be deprecated). why don't you just use getchar?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #34
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Nimitzhunter. 0 = there is no way you can go that way (look lower down in the code)
    1= airlock
    ""
    ""
    7= escape pods

  5. #35
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    No problem, happy to help

    Problem now is that you're calling print_house before you've declared it in the source file, so the compiler will guess at the parameters. Then it'll complain because you haven't supplied enough parameters. You can either move the whole print_house up above main, or you can put a function prototype somewhere before main. A function prototype is just a copy of what you've already got:

    Code:
    void print_house(unsigned int RoomNumber, unsigned int Direction); // note semicolon
    void main()
    {
     unsigned int RoomNumber = 1;
    ... etc
    You can put the prototype anywhere in the file so long as it's before main.

    It still won't work because you have print_house expecting 2 parameters: RoomNumber and Direction, but you're only calling it with 1: RoomNumber. Since you're not actually using Direction in print_house, you could get rid of the parameter. Or you could call it with both parameters.

  6. #36
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by L.Firth View Post
    Nimitzhunter. 0 = there is no way you can go that way (look lower down in the code)
    1= airlock
    ""
    ""
    7= escape pods
    Sure, but the point is that you declare array like this:
    Code:
    Location Room[7];
    This creates 7 elements numbered 0 to 6. It doesn't matter what numbers you give to the rooms -- you can't read from or write to Room[7]. If you are doing, and it's working, then that's by chance not by design. It's actually the worst case scenario in my opinion that it "just works" -- it WILL break eventually, and it's not fun to debug/track down.

  7. #37
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    My apologies, i made a mistake in only having 7 when i should of had 8. 0-7.

    In other news, thankyou Smokey Angel as it now works, all i have to do now is place the map in a different place on screen and i'm away. You have been a great help!

  8. #38
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    your print_house function also messes up the array.
    Code:
    printf("|    \\  Pod  %c    _                    |                |\n", Room_x[7]);
    Room_x[7] doesn't exist; it's whatever value exists in the memory location after Room_x[6]; so basically it's junk.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  9. #39
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Great! Glad it works.

    Quote Originally Posted by L.Firth View Post
    My apologies, i made a mistake in only having 7 when i should of had 8. 0-7.
    Hmmmm. Well, that'll solve the potential problem of memory access violations/segfaults, but it's not ideal, as you're taking up memory for 8 items when you only have 7. In the grand scheme of things it may seem a bit silly to worry about 500 or so bytes of space -- but IMO it's Bad Coding to take up more space than you need. Also, anyone modifying your program will expect the array to be indexed from 0. You've even made the mistake yourself:
    Code:
    for ( i=0; i<RoomNumbers; ++i ) {
    This will process Room[0]. What a waste of cpu time....okay, being silly, but.... you might want to change it to i = 1, but I think it's a bad habit to pick up.

    Looking at your code and the code of ShotgunChimp, who is doing the same thing, I can see you're both using 0 as a sentinel value to mean "Direction not valid". Since the code is identical I suspect it came from an instructor/teacher? That ........es me off - not only failing to teach you something, but also providing you with code that would need modification to do things properly. Unless there's something I'm missing... since other arrays are correctly indexed from 0 in the program.

    Grumble. Rant over.

Page 3 of 3 FirstFirst 123
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, 10:03 AM
  3. slight problem on win app tut
    By JustPlay4Fun in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2005, 10: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

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