Thread: char strings make me mad

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    char strings make me mad

    why did they make chars so hard to use? first it has to be constant, or a pointer, then there are terminating NULLs, differing quotes, arrgggggh!!!

    im making my own input routine, and everything is working except when 12 or more characters are entered. then when the final value is printed out at the end, it displays a version of the string where some of the characters are missing. eg, if you enter "01234567890", it displays the same, but "012345678901" returns an empty string! anything above 12 starts displaying weird strings, containing the correct characters but all messed up.

    here is some code:
    Code:
    char *temp;
    char *temp1;
    
    temp1="";
    temp="";
    that is the temp string where the inputed characters are added one by one, and another temp string to take the getch().

    Code:
    temp1[0]=getch();
    
    if (key is not an 'enter' or similar)
    
        dispText(temp1,font,sx,sy); //this routine just displays text 
        temp[len]=k; //len is current string length
        len++;           //increment len
        temp[len]=0; // set terminator
        sx+=6;          //graphics stuff;
        break;
    that code is for when a character is valid and is to be displayed and added to the temp string.

    im not sure but i think that its in that last bit of code where the error is. is there some rule of strings where you can only have 12 characters or something?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    > temp[len]=k; //len is current string length
    > len++; //increment len
    > temp[len]=0; // set terminator

    why are you assigning the char to the last character of the array?
    Last edited by The Dog; 07-27-2002 at 10:36 PM.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    len is the string length

    but chars start from index 0

    so if my string length is 4, the array goes char[0],char[1],char[2],char[3]. therefore, char[len] points to the space where the terminator is, or rather, the next available space for an inputed char to go.

    with that in mind, can you try and solve the other problems?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I'll need to see some more code.

  5. #5
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: char strings make me mad

    Originally posted by bennyandthejets
    why did they make chars so hard to use? first it has to be constant, or a pointer, then there are terminating NULLs, differing quotes, arrgggggh!!!

    im making my own input routine, and everything is working except when 12 or more characters are entered. then when the final value is printed out at the end, it displays a version of the string where some of the characters are missing. eg, if you enter "01234567890", it displays the same, but "012345678901" returns an empty string! anything above 12 starts displaying weird strings, containing the correct characters but all messed up.

    here is some code:
    Code:
    char *temp;
    char *temp1;
    
    temp1="";
    temp="";
    that is the temp string where the inputed characters are added one by one, and another temp string to take the getch().

    Code:
    temp1[0]=getch();
    
    if (key is not an 'enter' or similar)
    
        dispText(temp1,font,sx,sy); //this routine just displays text 
        temp[len]=k; //len is current string length
        len++;           //increment len
        temp[len]=0; // set terminator
        sx+=6;          //graphics stuff;
        break;
    that code is for when a character is valid and is to be displayed and added to the temp string.

    im not sure but i think that its in that last bit of code where the error is. is there some rule of strings where you can only have 12 characters or something?
    where did you allocate memory for temp and temp1? see me make the same mistake

    (if its done elsewhere in your code, ignore me)

    other than that, i see no problem with your code

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    can do

    i'll send the whole routine:

    Code:
    char *txtInput(int sx,int sy,FONT font)
    {
        char *temp1;
        char *temp;
        int i,j,k;
        int keyGroup;
        int xVal;
        int len;
    
        setVGA();
        temp1="0";
        temp="";
    
        len=0;
        xVal=0;
    
        while (!xVal)
        {
            while (1)
            {
                k=getch();
                if ( (k >= 97) && (k <= 122) )
                {
                    keyGroup=1;
                    break;
                }
    
                if ( (k >= 65) && (k <= 90) )
                {
                    keyGroup=2;
                    break;
                }
    
                if ( (k >= 48) && (k <= 57) )
                {
                    keyGroup=3;
                    break;
                }
    
                if (k==13)
                {
                    keyGroup=4;
                    break;
                }
    
                if (k==8)
                {
                    keyGroup=5;
                    break;
                }
            }
    
            temp1[0]=k;
    
            switch (keyGroup)
            {
                case 1:
                case 2:
                case 3:
                    dispText(temp1,font,sx,sy);
                    temp[len]=k;
                    len++;
                    temp[len]=0;
                    sx+=6;
                    break;
    
                case 4:
                    xVal=1;
                    break;
    
                case 5:
                    if (len==0) break;
                    sx-=6;
                    Wipe(sx,sy,5,8);
                    temp[len]=0;
                    len--;
                    break;
            }
        }
    
        return temp;
    
    }
    thats it. ill explain some of the functions:

    'Wipe' blacks out a triangle on the screen(this is mode 13)
    'dispText' displays a string at a certain point in a certain font

    if you know a better way of taking an input from getch(), then adding it to a temporary string, forget about my code and tell me!
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    thanks moi, your link helped
    it seems that it is important to declare a certain size for this purpose.

    dog, dont waste your time pouring over my code
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You could also allocate memory rather than statically assign memory to the string. You would do something like:

    Code:
    char *temp;
    
    temp = (char *)malloc(150);
    
    //do everthing I need to with the string
    
    free(temp); //deallocate the memory
    Whether you use malloc or use static allocation depends on what you are doing. If you will never need more than a certain amount of memory the static allocation will be just fine. But if you are reading a file or something, and you want a buffer that is exactly the same size as the file you would want to use malloc() (or c++'s new). Just make sure you free memory (for malloc() you use free() for new you use delete) so that you don't develope memory leaks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM