Like Tree1Likes

Confussion with Username

This is a discussion on Confussion with Username within the C Programming forums, part of the General Programming Boards category; Originally Posted by ShotgunChimp Am I doing something obviously wrong to cause this Access Violation? Yes Code: #define North=0 #define ...

  1. #16
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by ShotgunChimp View Post
    Am I doing something obviously wrong to cause this Access Violation?
    Yes

    Code:
    #define North=0
    #define East=1
    #define South=2
    #define West=3
    #define SIZE=20
    should be
    Code:
    #define North 0
    #define East 1
    #define South 2
    #define West 3
    #define SIZE 20
    but your compiler obviously doesn't care about that.

    Then there is still the out of bounds access of the Rooms array in SetupRooms().

    Valid indexes in your case are 0 .. 14 ( not 1 .. 15 )

    void main() is rather old fashioned.

    Kurt

  2. #17
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    I'm sure your problem has nothing to do with the name-related code.

    You must be using some archaic compiler. I can't even compile that code, and after I make the changes necessary to get it to compile, it runs, except that it refuses to go in anyplace other than "quit".

    1. I have to eliminate conio.h because that only works in Windows (and it's quite obsolete anyway).

    2. Your constant definitions should look like this:
    Code:
    #define North 0
    etc.

    3. main should always have a return type of int, not void.

    4. These printf statements are invalid:
    Code:
        printf(Room[Fig].RoomName);
        printf(Room[Fig].Description);
        printf(Room[Fig].Description2);
    Where are the format strings?

    5. In this:
    Code:
    for (long i=0; i<5e8; i++);   // Tony's code
    C shouldn't allow you to declare a variable inside a for loop

    6. Changed getch to getchar() since getch is not standard C

    After all of that, it compiles and runs without crashing, but as I said it doesn't go anyplace.
    Last edited by R.Stiltskin; 04-28-2012 at 11:41 AM.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,640
    I suggest:
    Code:
    enum direction {North, East, South, West};
    Then you can use this enum type instead of unsigned int. It won't actually give you any type safety, but it will make things a little more descriptive (plus avoid the possibility of accidentally declaring two direction values with the same integer value).

    Quote Originally Posted by R.Stiltskin
    C shouldn't allow you to declare a variable inside a for loop
    That is your personal opinion though. Apparently the C standard committee disagrees, so it has been allowed since C99.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    The origional Code that I gave at the very begging of this thread worked fine. Or atleast did on the compiler I'm using (CC386 IDE). I am completely at a loss. I added the code you gave me and I am now left with this Error. I don't understand why the programme only goes to quit. What would you do different to me because clearly my code has balls'ed up.

    Thanks for your ongoing help. It's invaluable!

  5. #20
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by ShotgunChimp View Post
    The origional Code that I gave at the very begging of this thread worked fine. Or atleast did on the compiler I'm using (CC386 IDE). I am completely at a loss. I added the code you gave me and I am now left with this Error. I don't understand why the programme only goes to quit. What would you do different to me because clearly my code has balls'ed up.

    Thanks for your ongoing help. It's invaluable!
    You're accessing the Room[15] that is out of bounds that means your program behaviour is undefined that means it could work fine one day, crash the other day blow up ur HD or whatever. It also means that it could run just fine. Now as you have added some code it just decided to exit with an Access Violation.
    Correct the errors and all will be fine.
    Kurt

  6. #21
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Just to reiterate again: C arrays are numbered from 0. There is no Room[15]. Accessing it could be the cause of the access violation -- just because it worked earlier doesn't mean it'll work now. Unrelated changes in the code can bring that sort of bug to light. Please don't access Room[15]!!

    I compiled it and had a roam around (after fixing a few bits and pieces to work with msvc) - it seems to work ok. These printf calls seem to be ok:

    Code:
    printf(Room[Fig].RoomName);
    printf(Room[Fig].Description);
    printf(Room[Fig].Description2);
    But I'd usually use
    Code:
    printf("%s", Room[Fig].Description2);
    I got a bit of garbage printed as a result of printing this:
    Code:
    strcpy(Room[15].Description, "\n\n%s stood in the centre of the road, fully aware of this shadowed eyes and \nslumped shoulders.");
    You have a %s but no string, so garbage ensues.

  7. #22
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    Thank you! It has solved the problem. It completely slipped my mind.

    Last thing, then I promise I'll leave you be, how do I now add the Username to the Room Discriptions. Where I put the %s before.

  8. #23
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    I have spent the last hour trying to work out how to take the user name, that has now been inputted, and place this in the other function (bellow). Ehhh?

    //ROOM 15
    strcpy(Room[14].RoomName, "Introduction");
    //I need the username here instead of 'He'
    strcpy(Room[14].Description,"\n\n He stood in the centre of the road, fully aware of this shadowed eyes and \nslumped shoulders.");
    strcpy(Room[14].Description2,"\nThe chill of night pinched his skin and woke him from his thoughts.\n\n");
    Room[14].Exit[North]=1;
    Room[14].Exit[East]=0;
    Room[14].Exit[West]=0;
    Room[14].Exit[South]=0;

  9. #24
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Hmm. Someone might have a cleaner idea than me! I assume you're going to want to print the user name in some of the other descriptions too?

    I'd put the %s back in the string:
    Code:
    strcpy(Room[15].Description, "\n\n%s stood in the centre of the road, fully aware of this shadowed eyes and \nslumped shoulders.");
    Then make sure the printf of description passes user as the second argument:
    Code:
    //DescribeRoom Function
    void DescribeRoom (int Fig, char* user)  // modify this to accept user as parameter
    {
    printf("Currently possition: ");
    printf(Room[Fig].RoomName); // don't have to pass it to printf if we don't want to
    printf(Room[Fig].Description, user); // just pass it when might be needed
    ...
    printf is a variadic function -- it can take variable numbers of arguments. It scans the first argument for format specifiers then reads whatever it's told to by the string. If there aren't any format specifiers then the arguments are ignored. So it's fine to pass user and not use it (slight overhead of marshalling the arguments for printf, but nothing major).

    Main problem with doing it this way it that it's difficult to extend. Say you had more information you wanted to print for some rooms but not others -- but 1 call to printf. It'd get painful pretty quickly -- so if you end up in that situation, try a different approach rather than fighting printf! Should be ok for now though, unless anyone has a better offer?

  10. #25
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    sprintf((Room[14].Description,"\n\n %s stood in the centre of the road, fully aware of this shadowed eyes and \nslumped shoulders.", user);
    You would have to pass user to SetupRooms()
    Kurt

  11. #26
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by ShotgunChimp View Post
    I have spent the last hour trying to work out how to take the user name, that has now been inputted, and place this in the other function (bellow). Ehhh?

    //ROOM 15
    strcpy(Room[14].RoomName, "Introduction");
    //I need the username here instead of 'He'
    strcpy(Room[14].Description,"\n\n He stood in the centre of the road, fully aware of this shadowed eyes and \nslumped shoulders.");
    strcpy(Room[14].Description2,"\nThe chill of night pinched his skin and woke him from his thoughts.\n\n");
    Room[14].Exit[North]=1;
    Room[14].Exit[East]=0;
    Room[14].Exit[West]=0;
    Room[14].Exit[South]=0;

    Ahh, yep, you could just copy it straight into the string, that's probably better actually.

    Pass user in as a char* like you've done before to SetupRooms()
    Then construct the string you need, I think:

    Code:
      strcpy(Room[14].Description,"\n\n ");
      strcat(Room[14].Description, user);
      strcat(Room[14].Description, "stood in the centre of the road, fully aware of this shadowed eyes and \nslumped shoulders.");

  12. #27
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    I have edited the following

    Code:
    void SetupRooms(char *user){
    but I have drawn a complete blank with your segestion to contruct the string. This is all kinds of wrong when I try to run it:

    Code:
    //ROOM 15
    strcpy(Room[14].RoomName, "Introduction");
    strcat(Room[14].Description, user); 
    strcat(Room[14].Description, "stood in the centre of the road, fully aware of this shadowed eyes and \nslumped shoulders.");
    strcpy(Room[14].Description2,"\nThe chill of night pinched his skin and woke him from his thoughts.\n\n");
    Room[14].Exit[North]=0;  
    Room[14].Exit[East]=20;  
    Room[14].Exit[West]=20;  
    Room[14].Exit[South]=20;
    edit: I don't get errors but again get an Access Violation...
    Last edited by ShotgunChimp; 04-28-2012 at 01:23 PM.

  13. #28
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Hmm, that's odd. Can you try
    Code:
    strcpy(Room[14].RoomName, "Introduction");
    strcpy(Room[14].Description, user); 
    strcat(Room[14].Description, "stood in the centre of the road, fully aware of this shadowed eyes and \nslumped shoulders.");
    strcpy(Room[14].Description2,"\nThe chill of night pinched his skin and woke him from his thoughts.\n\n");
    Room[14].Exit[North]=0;
    instead? I've just changed the strcat user to a strcpy. Strcat is for concatenating two strings, and we haven't written anything into Description yet. However your Room structs are all global so should have been zero initialised, so strcat should work straight off.

    If it's still failing, could you post the whole code again?

  14. #29
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    Quote Originally Posted by smokeyangel View Post
    If it's still failing, could you post the whole code again?
    Alas, I am still left with the same Access Violation. Before I add that code it works perfectly...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define North 0
    #define East 1
    #define South 2
    #define West 3
    #define SIZE 30
    int GetDirection (int RoomNumber);
    void DescribeRoom (int Fig);
    void DescribeExit (int Fig);
    void SetupRooms(); 
    typedef struct{
      char RoomName[50];
      char Description[400];
      char Description2[400];
      unsigned int Exit[4];
    }Location;
    Location Room[15];
    //Username Function
    void get_username( char *user, int size ) 
    {
        printf("What is your name?");
     fgets(user, size, stdin);
        printf("Your username is %s",user);
    }
    //Main function
    void main(){
     printf("A few few years ago you had found a book. Nothing special, but it kept his attention for a week or so. These days it was hard to find time to read, but you considered this the only way to cling onto civilisation. The book, in hindsight, was dull. You had no real interest interest in religion and waded through the pages with a sense of irony.");
     printf("\n\nYour memory of the past few days was somewhat confused. After so long it became hard to keep track of the passing of time. Events became blurred and actions became instinct. You stood at the top of a road, hands clutching the straps of your backpack, feet echoing that recognisable ache.\n\n\n\n");
     char user[SIZE];
     get_username( user, SIZE );
     unsigned int RoomNumber = 0; 
     unsigned int Direction;
     SetupRooms();
      while(1){
      
       system("cls");
       DescribeRoom(RoomNumber);
       DescribeExit(RoomNumber);
       Direction = GetDirection(RoomNumber);
       if(Direction==99)exit(0);
       if(Direction>0)RoomNumber= Direction;
      else
      {
      // Tony's code
      }
     }
    }
    
    //DescribeRoom Function
    void DescribeRoom (int Fig){
    printf("Currently possition: ");
    printf(Room[Fig].RoomName);
    printf(Room[Fig].Description);
    printf(Room[Fig].Description2);
    }
    
    //DescribeExit Function
    void DescribeExit (int Fig){
     printf("\n\nHe could go to any of the following places...\n");
     printf("\n--------------------------------------------------------------------\n");
     if(Room[Fig].Exit[North]!=0) printf ("\n(N) To the North he sees the %s",Room[Room[Fig].Exit[North]].RoomName);
     if(Room[Fig].Exit[East]!=0) printf ("\n\n(E) East is the %s",Room[Room[Fig].Exit[East]].RoomName);
     if(Room[Fig].Exit[South]!=0) printf ("\n\n(S) Looking South is the %s",Room[Room[Fig].Exit[South]].RoomName);
     if(Room[Fig].Exit[West]!=0) printf ("\n\n(W) Going West will take him to the %s",Room[Room[Fig].Exit[West]].RoomName);
     printf("\n\n--------------------------------------------------------------------");
     printf("\n\n\nJust type the first letter of the direction or press 'Q' to quit."); 
    }
    
    //GetDirection Function
    int GetDirection(int Fig)
    {
     char test;
     unsigned int NewDirection=0;
     while(1)
     {
      test=getch();
      if(test=='n') NewDirection=Room[Fig].Exit[North];
      if(test=='e') NewDirection=Room[Fig].Exit[East];
      if(test=='s') NewDirection=Room[Fig].Exit[South];
      if(test=='w') NewDirection=Room[Fig].Exit[West];
      if(test=='q') NewDirection=99;
      
      if(NewDirection!=20) return(NewDirection);
      printf("\n\nYou cannot go that way, there must be another path to take.");
     }
    }
    void SetupRooms(char *user){
    //ROOM 1
    strcpy(Room[14].RoomName, "Outside");
    strcpy(Room[14].Description, "\n\nHis eyes slowly rested back upon the house. Its unkept walls wore a thin green\ncoat of algae.");
    strcpy(Room[14].Description2, "\nThe trees produced shadows, dancing along the empty flower beds as a slow wind\nkissed the top of the branches. It looked sorry, lonely, friendly.\n\n");
    Room[14].Exit[North]=1;  
    Room[14].Exit[East]=0;  
    Room[14].Exit[West]=0;  
    Room[14].Exit[South]=0;  
    //ROOM 2
    strcpy(Room[1].RoomName, "Entrance");
    strcpy(Room[1].Description, "\n\nHe was greeted by a wide staircase, covered in what would have been a plush,\nwhite carpet.");
    strcpy(Room[1].Description2, "\nHis eyes were drawn up the stairs but his better judgment told him to look\naround the ground floor first.\n\n");
    Room[1].Exit[North]=6;  
    Room[1].Exit[East]=3;  
    Room[1].Exit[West]=2;  
    Room[1].Exit[South]=14;  
    //ROOM 3
    strcpy(Room[2].RoomName, "Office");
    strcpy(Room[2].Description, "\n\nHe slowly edged the door open, his left hand resting on the top of the blade\nhanging naturally off his hip.");
    strcpy(Room[2].Description2, "\nThe chair lay helplessly on the floor, paper strewn around its base. The room\nbore nothing of interest.\n\n");
    Room[2].Exit[North]=0;  
    Room[2].Exit[East]=1;  
    Room[2].Exit[West]=0;  
    Room[2].Exit[South]=0;  
    //ROOM 4
    strcpy(Room[3].RoomName, "Small Hallway 1");
    strcpy(Room[3].Description, "\n\nOn the other side of the hallway he came across two doors. The one to his left\nwas ajar, the other closed tightly.");
    strcpy(Room[3].Description2, "\nThe small hallway brought him a feeling of both comfort and clostrophobia.\n\n");
    Room[3].Exit[North]=4;  
    Room[3].Exit[East]=5;  
    Room[3].Exit[West]=1;  
    Room[3].Exit[South]=0;  
    //ROOM 5
    strcpy(Room[4].RoomName, "Downstairs Bathroom");
    strcpy(Room[4].Description, "\n\nThe room was the same as the day it was left. It was not big, or luxurious, but instead a reminder of what had been.");
    strcpy(Room[4].Description2, "\nHe spent a minute searching the shelves, but a man in his position found little use for aftershave.\n\n");
    Room[4].Exit[North]=0;  
    Room[4].Exit[East]=0;  
    Room[4].Exit[West]=0;  
    Room[4].Exit[South]=3;  
    //ROOM 6
    strcpy(Room[5].RoomName, "Downstairs Bedroom");
    strcpy(Room[5].Description, "\n\nThe cream walls adorned with posters, pictures and memories all suggested that\nof a youth");
    strcpy(Room[5].Description2, "\nA double bed lay half dressed with belongings hurriedly strewn across it.\nMusical instruments, a guitar, a drum set.\n\n");
    Room[5].Exit[North]=0;  
    Room[5].Exit[East]=0;  
    Room[5].Exit[West]=3;  
    Room[5].Exit[South]=0; 
    //ROOM 7 
    strcpy(Room[6].RoomName, "Landing");
    strcpy(Room[6].Description, "\n");
    strcpy(Room[6].Description2, "\n");
    Room[6].Exit[North]=10;  
    Room[6].Exit[East]=8;  
    Room[6].Exit[West]=7;  
    Room[6].Exit[South]=1;
    //ROOM 8
    strcpy(Room[7].RoomName, "Living Room");
    strcpy(Room[7].Description, "\nBLAH\n\n");
    Room[7].Exit[North]=0;  
    Room[7].Exit[East]=6;  
    Room[7].Exit[West]=0;  
    Room[7].Exit[South]=0; 
    //ROOM 9
    strcpy(Room[8].RoomName, "Kitchen");
    strcpy(Room[8].Description, "\nBLAH\n\n");
    Room[8].Exit[North]=0;  
    Room[8].Exit[East]=9;  
    Room[8].Exit[West]=6;  
    Room[8].Exit[South]=0;
    //ROOM 10
    strcpy(Room[9].RoomName, "Utility Room");
    strcpy(Room[9].Description, "\nBLAH\n\n");
    Room[9].Exit[North]=0;  
    Room[9].Exit[East]=0;  
    Room[9].Exit[West]=8;  
    Room[9].Exit[South]=0;  
    //ROOM 11
    strcpy(Room[10].RoomName, "Upstairs Hallway");
    strcpy(Room[10].Description, "\nBLAH\n\n");
    Room[10].Exit[North]=12;  
    Room[10].Exit[East]=13;  
    Room[10].Exit[West]=11;  
    Room[10].Exit[South]=6;  
    //ROOM 12 
    strcpy(Room[11].RoomName, "Bedroom 1");
    strcpy(Room[11].Description, "\nBLAH\n\n");
    Room[11].Exit[North]=0;  
    Room[11].Exit[East]=10;  
    Room[11].Exit[West]=0;  
    Room[11].Exit[South]=0;  
    //ROOM 13 
    strcpy(Room[12].RoomName, "Bedroom 2");
    strcpy(Room[12].Description, "\nBLAH\n\n");
    Room[12].Exit[North]=0;  
    Room[12].Exit[East]=0;  
    Room[12].Exit[West]=0;  
    Room[12].Exit[South]=10; 
    //ROOM 14
    strcpy(Room[13].RoomName, "Upstairs Bathroom");
    strcpy(Room[13].Description, "\nBLAH\n\n");
    Room[13].Exit[North]=0;  
    Room[13].Exit[East]=0;  
    Room[13].Exit[West]=10;  
    Room[13].Exit[South]=0;   
    //ROOM 15
    strcpy(Room[0].RoomName, "Introduction"); 
    strcpy(Room[0].Description, user);  
    strcat(Room[0].Description, "stood in the centre of the road, fully aware of this shadowed eyes and \nslumped shoulders."); 
    strcpy(Room[0].Description2,"\nThe chill of night pinched his skin and woke him from his thoughts.\n\n");
    Room[0].Exit[North]=14;  
    Room[0].Exit[East]=0;  
    Room[0].Exit[West]=0;  
    Room[0].Exit[South]=0;   
    
    }
    Here's the code with the problem.

  15. #30
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Your compiler shouldn't let you get away with that!!

    You have:
    Code:
    void SetupRooms(char *user){
    Which is correct, but the prototype at the top of the file doesn't take any arguments. And most importantly, the call from main doesn't actually pass user to SetupRooms. So the pointer will be invalid/garbage/wrong. You need to update all the declarations with the parameter and make sure you actually pass it.

    Just FYI, since your compiler doesn't seem to diagnose much, here are the current warnings from MSVC (with the above fixed).

    Code:
    \temp3.c(17): warning C4820: '<unnamed-tag>' : '2' bytes padding added after data member '<unnamed-tag>::Description2'
    \temp3.c(36): warning C4127: conditional expression is constant
    \temp3.c(39): warning C4365: 'argument' : conversion from 'unsigned int' to 'int', signed/unsigned mismatch
    \temp3.c(36): warning C4127: conditional expression is constant
    \temp3.c(39): warning C4365: 'argument' : conversion from 'unsigned int' to 'int', signed/unsigned mismatch
    \temp3.c(40): warning C4365: 'argument' : conversion from 'unsigned int' to 'int', signed/unsigned mismatch
    \temp3.c(41): warning C4365: 'argument' : conversion from 'unsigned int' to 'int', signed/unsigned mismatch
    \temp3.c(41): warning C4365: '=' : conversion from 'int' to 'unsigned int', signed/unsigned mismatch
    \temp3.c(76): warning C4127: conditional expression is constant
    \temp3.c(78): warning C4242: '=' : conversion from 'int' to 'char', possible loss of data
    \temp3.c(85): warning C4365: 'return' : conversion from 'unsigned int' to 'int', signed/unsigned mismatc
    Might be worth having a look at those -- it's in maximum whinge mode (/Wall) so some are not problems, but the signed/unsigned stuff might be worth checking.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. username to uid
    By chrismiceli in forum Linux Programming
    Replies: 1
    Last Post: 07-30-2007, 01:23 PM
  2. Birthyear in username? Why?
    By brewbuck in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-28-2007, 06:48 PM
  3. username collection
    By algi in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2005, 01:20 PM
  4. Username function
    By jamie85 in forum C Programming
    Replies: 6
    Last Post: 03-19-2004, 06:33 PM
  5. getting username from Windows NT
    By Flucas in forum Windows Programming
    Replies: 1
    Last Post: 10-29-2001, 06:39 PM

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