String question

Discussion in 'Game Development (Technical)' started by Hidden Sanctum, Jun 28, 2006.

  1. Hidden Sanctum

    Original Member

    Joined:
    Nov 12, 2005
    Messages:
    66
    Likes Received:
    0
    I've got all sorts of stuff going on in my game right now with special effects and things like that, but I am stuck on the simplest of things.

    User Profiles. I am good to go with adding, deleting, loading, users keying in their name, etc. However, I can't for the life of me clear a string in C.

    I have a variable:

    char tempUserName[10];

    Works great the first time you use it, but the second time you do, the previous name is still in there.

    I tried to use the VB technique of making it equal to "" - although using strcpy(tempUserName,"").That didn't work because it only wiped clean the first character.

    The I figured I'd do strcpy(tempUserName," ") with ten spaces, but then the spaces get saved as well leaving the users name off center on the screen.

    Trying to use the clear() function doesn't work (I'm assuming because I am using char instead of string.)

    I know this is something really easy, but the solution has eluded me. Ten years of VB programming has spoiled me.

    Any advice with this would be greatly appreciated.
     
  2. Ryan Clark

    Indie Author

    Joined:
    Oct 29, 2004
    Messages:
    656
    Likes Received:
    0
    A C-style string is null terminated. To "clear" it, do this:

    tempUserName[0] = '\0';

    That will place a null character at the beginning of the string, effectively making it "empty".

    I hope this helps!
     
  3. Slayerizer

    Original Member

    Joined:
    Dec 13, 2004
    Messages:
    103
    Likes Received:
    0
    it would be easier for you to work with the 'string' datatype instead of 'char'.

    but if you insist working with your char array, you can use this:

    Code:
       memset(tempUserName,0,sizeof(tempUserName);   
    be aware that when you declare
    char tempUserName[10];

    you're limiting yourself to an array of 10 characters only, so if you attempt to copy a string longer than 10 characters into your Tempusername, you will corrupt your memory buffer (and your program).

    here is a string tutorial with the string class:
    string tutorial

    with the string class you can empty a string with : tempusername = "";

    if you insist working with 'C style' char, have a look here on page 3 you have a listing of the functions, including MEMSET.
     
  4. Hidden Sanctum

    Original Member

    Joined:
    Nov 12, 2005
    Messages:
    66
    Likes Received:
    0
    Wow, you guys are quick. I should have done this last night instead of fighting it for 2 hours like I did.:(

    Ryan - I did try the \0 when trying to figure it out, but used a different formatting which after seeing yours explains what I did wrong.

    Slayerizer - The 10 character limit is intentional for character spacing reasons on the screen and is forced, so there shouldn't be any issues (famous last words...)with trying to copy a string onger than 10 characters.

    Thanks for you help! :)
     
  5. Applewood

    Moderator Original Member Indie Author

    Joined:
    Jul 29, 2004
    Messages:
    3,859
    Likes Received:
    2
    You also might want to consider a little macro like this:
    Code:
    #define		ClearStruct(obj)	memset(&(obj),0,sizeof(obj))
    
    Then you can do ClearStruct(anything) and it'll get cleaned out to zeroes. I use it everywhere. (Though I changed my own memory manager call back to memset for clarity).
     
  6. svero

    Moderator Original Member Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    3,392
    Likes Received:
    6
    Just a note on that 10 character limit comment. The limit is actually 9 characters for the name + one terminating character. Copying a name that's 10 characters long into the string will write a null a the 11th position in memory to terminate the string, and that would be out of bounds causing all kinds of mischief. Best way to avoid that sort of issue altogether is to use a good string class with boundary checks and asserts. Char arrays are just asking for trouble.
     
  7. MibUK

    Original Member

    Joined:
    Feb 8, 2005
    Messages:
    84
    Likes Received:
    0
    Hidden Sanctum.

    Dont forget that the 10 character limit you've given yourself is actually a 9 character visible character limit.
    Every char* style string in C has a null on the end, so the string "1234567890" would return 10 when doing strlen on it, but would need a char buf[11] to store it.
    Thats because the string is actually the data "1234567890\0" witht eh extra null on the end.
    What would happen if you happened to put an actual 10 character string into that buffer would vary depending on when it happened and how your compiler has arranged the memory. It might go unnoticed for ages, giving you just the occasional unexplained crash. (Or worse, occasionally reset another variable to 0, giving just unexplained weirdness!).

    Seriously, unless you actually need to do this in C rather than C++, then use std::string.

    Usage is something like this...

    std::string mystring;
    mystring = "Bob";
    mystring = "Anne";
    mystring.append(" Janeson");
    mystring.clear();
    int stringLength = mystring.length();


    If you need to pass a char* to soemthing (like an api), you can use mystring.c_str() to get a char*. (Don't let anyhting modify that though).

    C++ style std::string is always preferable to the C style char* in almost all cases.
     
  8. svero

    Moderator Original Member Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    3,392
    Likes Received:
    6
    Beat ya to it :p
     
  9. Slayerizer

    Original Member

    Joined:
    Dec 13, 2004
    Messages:
    103
    Likes Received:
    0
    addendum

    to avoid any possible issues, I have no choice to recommend strncpy instead of strcpy. With strncpy, you can force the maximum string length to copy in the new buffer.

    please note that if you declare

    Code:
    char buffer[10];
    
        strcpy(buffer,"abcdefghij");
        buffer[0]=0;   // or buffer[0]='\0';
        strcpy(buffer,"hello");
    
    your buffer will looks like it's empty but it won't
    it will have some leftovers.

    temp -> "hello\0ghij";

    you may not have a problem with that, since the \0 with stop the built-in string functions. I prefer to personnaly clear the whole array because I often save my stuff in a binary format and I dont want to see leftovers in my file.

    both ways, it will work.

    NOTE:

    if you really need to store an array of 10 characters, you need to declare a char array of size 11 to hold the string terminator (character 0 or '\0').

    so for storing 10 characters, you need 10 bytes + 1 extra for the string terminator.
     
  10. Hidden Sanctum

    Original Member

    Joined:
    Nov 12, 2005
    Messages:
    66
    Likes Received:
    0
    My bad on my explanations. I went back and double-checked my code. Yes, I am limiting input to 9 characters, not 10 and leaving room for the termination character. I'll blame that on starting it and not finishing it so I could work on the fun stuff and I forgot that they were only allowed to type in 9 characters.

    The initial limit was based on the fonts I was using which were a bit wide. I've since changed them to something tighter which leaves a bit more room.

    How many characters do you guys allow players to type their name in? Does 9 seem too small?

    BTW, thanks for all the tips. They have been exceedingly helpful.
     
  11. Slayerizer

    Original Member

    Joined:
    Dec 13, 2004
    Messages:
    103
    Likes Received:
    0
    I would personally suggest 15.. I like to enter : 'Luc is the best'

    you could also do this:


    Code:
    const int TextAreaWidth=600; //width of text area
    const int FontWidth=10;    //width of font in pixel
    const int SpacingWidth=5;  //specify 5 pixels margin
    const int MaxNameLength= ceil((TextAreaWidth-SpacingWidth*2)/FontWidth); 
    and then use MaxNameLength in your code

    like

    char *PlayerName;
    PlayerName=new char[MaxNameLength+1]; // +1 for null terminator
    //blabla
    delete []PlayerName;


    if you have different string lengths that you want to keep track you could do this:

    Code:
    
    
    
    enum E_NAME_LENGTH
    { 
       ENL_PlayerName=0, 
       ENL_MenuItems, 
       ENL_MenuTitle, 
       ENL_DUMMY_TERMINATOR 
    };
    
    //the dummy has a value of 4, so we create an array of 3
    int MaxStringLength[ENL_DUMMY_TERMINATOR-1]; 
    
    
    MaxStringLength[ENL_PlayerName]=16;
    MaxStringLength[ENL_MenuItems]=30;
    MaxStringLength[ENL_MenuTitle]=40;
    
    then you can use MaxStringLength[<object name>] anywhere in your code!
    
    if you need to insert more types, insert them just before the dummy item..
    
    
    my 0.02$
     
    #11 Slayerizer, Jun 28, 2006
    Last edited: Jun 28, 2006
  12. HappyCat

    Original Member

    Joined:
    Jun 28, 2005
    Messages:
    54
    Likes Received:
    0
    I actually limited it by size on-screen. I use a proportional font and each time the player enters a letter I add it to the string and see if the on-screen representation of the string is still within the size limit (300 pixels or whatever) - if not I reject the letter. So I don't have a hard limit on letters, but I can still be sure everything will fit nicely on the screen.
     
  13. Rebrehc's Industries

    Original Member

    Joined:
    Feb 11, 2005
    Messages:
    52
    Likes Received:
    0
    Lately, I have seen a lot of games with a 7 character limit, which is way too small. As for a 9 character limit, or even 10, Christopher is a fairly common name and it is 11 characters.
     
  14. Fabio

    Original Member

    Joined:
    Sep 30, 2005
    Messages:
    499
    Likes Received:
    0
    My brother's name is 12 (plus the terminator :) ).

    Let's not forget all the composed (sp?) names as well.. some people's name may be "Jean Christophe" as well, and maybe (s)he likes to type it all.

    Well, probably modern computers are powerful enough to handle more than 7 characters anyway. If not, I would stick with the coin op ~standard 3 chars.. rather than impose an arbitrary-looking limit like 7 (although it's a nice number :) ).

    Nowadays that all displays are ultra-high-res by yesterday's standards (the Windows Screen control panel doesn't even let you set the desktop to anything less than 640x480) maybe one should impose the limit of 3 chars on computers with less than 1K of RAM and otherwise make it virtually limitless or give this illusion to the user. :D
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer