Thread: Can't seem to quite get this function to work...(should be easy)

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    26

    Can't seem to quite get this function to work...(should be easy)

    Okay, so I have created a class MyString, and in it, I have a copyTo function. It has a starting position, an ending position, and the string it's supposed to be copied to.

    Here's the function call from another class in the program:

    Code:
     Address.CopyTo(0, atPosition, LocalPart.UserName);
    Address is the whole string that I'm testing it, and LocalPart.UserName is a Mystring.

    and here's the function itself:

    Code:
    void MyString::CopyTo(int startPosition, int length, MyString &destination)
    {
    	while( startPosition != length )
           {
                 destination.Buffer[startPosition] = Buffer[startPosition];
                 startPosition++;
    	}
    
    }
    Buffer is a member of the MyString class, and the ONLY member, it is a char *

    Whenever I try to do this, it comes out with length 71...which has nothing to do with any part of the program...at all.

    Doing some random crap, I've gotten it to say 24, which is the size of the string I'm testing it with atm, but nothing to what it's supposed to be. :|

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    So you're saying that length is the wrong value? In that case, atPosition must also be the wrong value (since that is what you pass to the copyTo function as the length argument), in which case there's no way for us to tell what's going wrong without seeing more code.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    26
    nope, that's not the problem, I did a debug statement to print atPosition...it's right (8 for this particular string).

    Also, just to make sure, I hardcoded 8 into the call of copyTo

    there's something wrong with the function...

    it comes up with 71 as the length of the new string. the original is only 24...

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    > it comes up with 71 as the length of the new string

    "it"? Are you saying you set a break statement inside CopyTo and a watch on the variable "length" and found that "length" was 71? As far as I can see, there isn't any problem with your code (although it might be used incorrectly)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    26
    I think it is just used incorrectly.

    after

    Code:
     Address.CopyTo(0, atPosition, LocalPart.UserName);
    I have

    Code:
    int length1 = LocalPart.UserName.GetLength();
       cout << length1;

    and it prints out 71...


    EDIT: Okay, maybe it's something else, because I just changed the function to

    Code:
    void MyString::CopyTo(int startPosition, int length, MyString &destination)
    {
    	for(int a=0; a<9; a++)
        {
            destination.Buffer[startPosition] = Buffer[startPosition];
            startPosition++;
    	}
    
    }
    and it still prints out 71....
    Last edited by Saggio; 10-04-2006 at 06:39 PM.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well, how are you calculating the length of the string? If you're looking for a '\0', then you'd better make sure the array is initialized with nulls to begin with. You also should add one at the end when you copy (if the new string is shorter, you'll wind up with a longer length then you expect). Or perhaps you should store the length in the class.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    pet peeve. The names of your local variables are really lame. They don't really designate what they refer to. For example, startPosition is only the start position for the first buffer assignment, and then you're incrementing it. I would choose a name like "begin" or "pos". You have a variable length, but it isn't really the length, it's the end position. Why not choose a name like "end" or "endpos"?

    Finally, you say that the length of the destination is not right, but do you have a member function that returns the length? Can you provide the code for it? Does it just use strlen()? If so you're copyto might be overwriting the null terminator in the destination.

    For example, there is so little in the way of testing the inputs, I can imagine a case where your end position is not the null terminator in the source string, (just a mid section in the source string being copied) and you're actually overwriting the null terminator in the destination string (say the destination is a length smaller than the source string). So you end up with a destination with no null terminator. When you call strlen against it, there's no telling when or where it will find a null byte. I bet this is what is happening actually...
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    26
    alright, I got it, adding a NULL char at the end of the loop fixed it.

    Thanks! you jsut saved my butt

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    26
    IfYouSaySo, I understand what you're talking about with the vars...all Var names are specified by my professor, I have no control over that.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    26
    Sorry, for all of the replies next to each other, but there is something wrong with the function it seems, but it's getting the correct length now.

    here's the function as it stands now, with a debug cout statement:

    Code:
    void MyString::CopyTo(int startPosition, int length, MyString &destination)
    {
    	while( startPosition != length )
        {
            destination.Buffer[startPosition] = Buffer[startPosition];
            startPosition++;
            cout << destination.Buffer[startPosition];
    	}
        destination.Buffer[startPosition] = '\0';
        cout << endl;
    
    }
    and here's what it outputs:

    ╠╠╠
    ╠╠╠╠╠╠╠╠
    sf
    delphia

    it SHOULD be:

    asf
    adelphia.net


    wtf?

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    903
    You are incrementing before you output. Switch the second and last lines of the while() loop. That should do the job.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    26
    okay, here's what I have:

    here's the test string:

    "[email protected]"

    I have these calls: The first just copies the string and prints it, the second gets everything before the @ (not including it), and the second gets everything after the @ (not including it).

    Code:
    Address.CopyTo(0, length, LocalPart.Strng);
    
    
       Address.CopyTo(0, atPosition, LocalPart.UserName);
       Address.CopyTo(atPosition + 1, length - atPosition - 1, Domain.Name);

    atPosition is the location of the '@' symbol. Address is said String.

    and here's my CopyTo functions:
    Code:
    void MyString::CopyTo(int startPosition, int length, MyString &destination)
    {
    	while( startPosition != length )
        {
            destination.Buffer [ startPosition ] = Buffer [ startPosition ];
            cout << destination.Buffer[startPosition];
            startPosition++;
    	}
        destination.Buffer [ startPosition ] = '\0';
        cout << endl;
    
    }

    and here's the output:

    [email protected]
    asf
    adhia

    see, for some reason, it's not getting the '.net', yet if I print out the length for Domain.Name, it still prints 9 (which is correct)...


    it SHOULD be:
    [email protected]
    asf
    adhia.net

    what could be going wrong here?


    EDIT: I forgot to mention, if I try anything more positive than length - atPosition - 1 for the second CopyTo function call, it prints it out, but then crashes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM