Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    paris, france
    Posts
    1,216
    Thanks
    0
    Thanked 0 Times in 0 Posts

    CPP strings and user input

    hi peeps

    i'm currently writing a small program that has a file system class (called C_fsystem)

    this is it's code

    [CODE]
    class C_fsystem
    {
    protected:
    char SZ_fpath [];
    char SZ_fname [];
    char SZ_fexten [];
    public:
    void setPath(char* TMP_buffer){strcpy(SZ_fpath, TMP_buffer);}
    void setName(char* TMP_buffer){strcpy(SZ_fname, TMP_buffer);}
    void setExten(char* TMP_buffer){strcpy(SZ_fexten, TMP_buffer);}
    char fullName();
    C_fsystem(char*, char*, char*);
    };

    C_fsystem::C_fsystem(char* PARAM_path, char* PARAM_name, char* PARAM_exten)
    {
    strcpy(SZ_fpath, PARAM_path);
    strcpy(SZ_fpath, PARAM_name);
    strcpy(SZ_fpath, PARAM_exten);
    }

    char C_fsystem::fullName()
    {
    char TMP_path [1000];
    strcpy(TMP_path, SZ_fpath);
    strcat(TMP_path, SZ_fname);
    strcat(TMP_path, ".");
    strcat(TMP_path, SZ_fexten);
    }

    int main()
    {
    C_fsystem myfile("D:\\my documents 2\\robin\\cpp sources\\terraserve\\", "main", "cpp");
    cout<<myfile.fullName();
    cin.get();
    exit(0);
    }
    [CODE]

    now everything compiles well, but i get a wierd symbol on my screen instead of the path, like i was expecting.

    i'm getting the impression ive got compeltly the wrong end of the stick concerning strings.

    another problem is, how do i initalise strings/char arrays so they can be any length? do i have to use dynamic memeory or not?
    photoshop too expensive? use the GIMP! www.gimp.org

  • #2
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it happens because your fullName function does not return anything!
    Code:
    // return type should be a string, not a char.
    char C_fsystem::fullName()
    {
    char TMP_path [1000];
    strcpy(TMP_path, SZ_fpath);
    strcat(TMP_path, SZ_fname);
    strcat(TMP_path, ".");
    strcat(TMP_path, SZ_fexten);
    // missing return value here - you cannot return TMP_path as its a local variable. 
    // consider rewriting this function in a different way.
    }
    Nobody is Perfect. I am Nobody.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    paris, france
    Posts
    1,216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    doh! i spent so much time fidling with stings i thought it would be that.

    why can't i return TMP_path though? what is it lcoal to? the function or the class. in what does it differ to this for example(from cplusplus.com tutorial)

    #include <iostream.h>

    int addition (int a, int b)
    {
    int r;
    r=a+b;
    return (r);
    }

    int main ()
    {
    int z;
    z = addition (5,3);
    cout << "The result is " << z;
    return 0;
    }

    r is local to the function but it is still returned

    thanks
    photoshop too expensive? use the GIMP! www.gimp.org

  • #4
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It is local to the function. When you return a string, only the reference to the string is returned. so in essence, you are returning a pointer to a local memory location which will not be available after the function call!

    In your example, you are returning an int. and when you return an int, it returns the integer and not a reference to it. To apply the analogy, you can return a char in that way, but not a string.
    Nobody is Perfect. I am Nobody.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •