Thread: help with *parametres. (?)

  1. #1
    Unregistered
    Guest

    help with *parametres. (?)

    int strpartcopy(char* goal,char* source,int sTART,int eND){
    if(sTART>eND)
    return 0;
    else{
    int i=0;
    for(;sTART<=eND;++sTART,++i)
    goal[i]=source[sTART];
    goal[i]='\0';
    return 1;
    }
    }

    I hoped to copy parts of a String with strpartcopy, for
    example:
    char arr1[12]="BIG TROUBLE";
    char arr2[6]="SO OLD";
    strpartcopy(arr1, arr2, 3, 5) // Should lead to
    // "OLD TROUBLE" in arr1... BUT:
    cout << arr1; // only shows "OL" or "OLD" on the screen
    /* What' s wrong? I hoped to get "OLD TROUBLE" */

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    We rarely get what we hoped for, try doing a paper run of your program since that function is so small. Take a piece of paper and a pencil and write down the starting values of your variables, then start running through the function with the desired input (assume that the input is correct for now) while changing the values where your code changes them. If you systematically and logically run through the code you wrote then you will quickly find the problem.

    I recommend this for any small function, it helps you fully understand the logic and find any bugs without having to deal with an awkward debugging program.

    I will note one thing real quick though
    int i=0;
    for(;sTART<=eND;++sTART,++i)
    This doesn't make sense, you can easily do this and save a line as well as keep from confusing the reader.
    for(int i = 0; sTART<=eND; ++sTART, ++i)

    -Prelude
    Last edited by Prelude; 12-22-2001 at 07:49 PM.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    When displaying a char array ( a string) as soon as a \0 is found, that is the end of the string.
    You insert one at the end of the copy function.

  4. #4
    Unregistered
    Guest
    '\0' - that's it!
    thankyousomuch

Popular pages Recent additions subscribe to a feed