Like Tree3Likes

Slight Confusion

This is a discussion on Slight Confusion within the C Programming forums, part of the General Programming Boards category; Are you all in the same class? http://cboard.cprogramming.com/c-pro...g-program.html http://cboard.cprogramming.com/c-pro...-username.html http://cboard.cprogramming.com/c-pro...-im-stuck.html Because every one of you seems to be writing some ...

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Are you all in the same class?
    help finishing this program
    Confussion with Username
    http://cboard.cprogramming.com/c-pro...-im-stuck.html

    Because every one of you seems to be writing some kind of text adventure, and EVERY one of you has screwed up on thinking arrays start at 1, whereas in fact they start at zero.

    Now one making the mistake is the students fault.
    But 4 students with the same mistake - now that's down to crummy teaching.
    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.

  2. #17
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Zuk - would it? I would have thought you'd have to write
    Code:
    #define North North=1
    to get it to substitute like that.

    In any case I'm surprised it didn't give an error straight away.


    Quote Originally Posted by L.Firth View Post
    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];
    Ok, right, you can't do that. In C you can only initialise an array with a compiler-time constant: i.e. the compiler has to know what the value of RoomNumbers is, and it can't do that with a variable passed in as a parameter. You can dynamically allocate memory using malloc(), but I don't think there's any need for that here as you do know the required length of the array.

    Quote Originally Posted by L.Firth View Post
    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.
    Ok, I understand. But the number of rooms is fixed, there are only 7 of them. So why not just use:

    Code:
    void print_house(unsigned int RoomNumber, unsigned int Direction){ // set up Room numbers and Directions
     char Room_x[7];
     unsigned int i;
     for ( i=0; i<7; ++i )
      Room_x[i] = ' ';
     Room_x[RoomNumber] = 'X';
    Accessing a particular room number with [RoomNumber] is fine - it's just the declaration you can't do.

    Or far nicer (using magic numbers like "7" in code is bad for maintainability, readability, etc), do as Zuk suggested and put

    Code:
    #define RoomNumbers 7
    At the beginning.

  3. #18
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by Salem View Post
    Now one making the mistake is the students fault.
    But 4 students with the same mistake - now that's down to crummy teaching.
    I noticed that too. Bad teacher!!

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ZuK View Post
    If the op has followed my advice RoomNumbers should be a #define for 7
    Go read my post again. I didn't ask what RoomNumbers was. I asked what RoomNumber was.


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

  5. #20
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by smokeyangel View Post
    Zuk - would it? I would have thought you'd have to write
    Code:
    #define North North=1
    True, I don't know how it would be substituted but it should be an error anyway
    Kurt

  6. #21
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Salem - Yes we are. But we're gonna try and do what we can. I know we are a pain but we are essentially trying to teach ourselves from the ground up, afterall we are all product designers given a programming project oddly.

    Ok, so i've updated the relevant section of code to:
    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] = ' ';
     }
     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");
    }
    But i get an internal error.

    I plan to update the #define RoomNumbers 7 part but i'm still a little confused.

  7. #22
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    But i get an internal error.
    This is not specific enough! How about the full text of the error and the line on which it occurs?

    Code:
    char Room_x[RoomNumber];
    Does the compiler you're using support variable-length arrays?

  8. #23
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    I will give the whole of what my compiler CC386 IDE is giving me in return when i build:

    Compiling CMAP.c
    Linking
    Compile Done. Errors: 0, Warnings: 0
    Internal Error...

    And that is all. I can't give you any more detail as i don't have it myself. Sorry.

  9. #24
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    My code as it stands:
    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
    #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");
    }

  10. #25
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Yeah, you're still not allowed to do that in C.

    RoomNumber is the number of the room you want to print 'X' in, right?
    And RoomNumbers is the total number of rooms.

    So now, if it was legal syntax, you'd create an array with only "RoomNumber" entries, which probably isn't what you want. You want to:
    Create an array with 1 char for each room
    Loop through every character
    If the room number matches the one given, put 'X' in the array
    otherwise put ' ' in the array

    This line
    Code:
    char Room_x[NUM];
    creates the array. NUM needs to be a constant -- either #defined, or just a number, or a const. You can't put a function parameter in here.

    This'd be easier if the compiler would give you a proper error message!

  11. #26
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Disregard my last post, you posted before me

  12. #27
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by L.Firth View Post
    I will give the whole of what my compiler CC386 IDE is giving me in return when i build:

    Compiling CMAP.c
    Linking
    Compile Done. Errors: 0, Warnings: 0
    Internal Error...

    And that is all. I can't give you any more detail as i don't have it myself. Sorry.
    Wow. Helpful toolchain award goes to.....CC386

    So it says "Compile Done" before "Internal Error". Did it manage to finish linking? Was an executable file produced?

    Is it possible the IDE started running/debugging the program? You've still got out-of-bounds array accesses which could cause dodgy behaviour.

    I checked your code on MSVC - it compiles fine now.

  13. #28
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    I do not believe the file was produced, when i try to run it simply runs what looks like the last working .exe file it produced was (before the map sections were added). I tried deleting the existing .exe and then tried again. This time it produced a new .exe but still, nothing to do with the map or anything like that. Its as if the program has completely ignored everything to do with print_house onwards.

  14. #29
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    You don't call print_house anywhere in the code you pasted. You're definitely calling it, right?

    Does the IDE have a "clean" option? It'll probably be near whatever menu/button you use to build. If it does, try that then try building again. If not, see if there are any *.o or *.obj files around where the exe was, and delete them. The compiler outputs these (object) files, then the linker then puts them together into an exe. So it is possible that the code hasn't been regenerated.

  15. #30
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Still got some warnings/errors
    Code:
    kurt@kurt-virtual-machine:~/c_tests$ gcc pp.c -Wall --pedantic
    pp.c: In function 'DescribeRoom':
    pp.c:65:2: warning: format '%d' expects a matching 'int' argument [-Wformat]
    pp.c:66:2: warning: format not a string literal and no format arguments [-Wformat-security]
    pp.c:68:2: warning: format not a string literal and no format arguments [-Wformat-security]
    pp.c: In function 'GetDirection':
    pp.c:86:3: warning: implicit declaration of function 'getch' [-Wimplicit-function-declaration]
    pp.c: At top level:
    pp.c:98:6: warning: return type of 'main' is not 'int' [-Wmain]
    pp.c: In function 'print_house':
    pp.c:113:68: warning: C++ style comments are not allowed in ISO C90 [enabled by default]
    pp.c:113:68: warning: (this will be reported only once per input file) [enabled by default]
    /tmp/ccnim8E1.o: In function `GetDirection':
    pp.c:(.text+0x5e3): undefined reference to `getch'
    Kurt

Page 2 of 3 FirstFirst 123 LastLast
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, 07: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, 09:02 AM
  5. Slight problem...
    By Fordy in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 08-10-2002, 02:32 PM

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