Help with string class

Discussion in 'C++' started by Doug Goulden, Feb 19, 2005.

  1. Doug Goulden

    Doug Goulden Guest

    I have an application library that I have been using for some time with no
    problems. Yesterday though I started to use part of the library and found
    that the results were much different than I expected. My problem is in using
    the std::string class, when I create a string by calling its constructor or
    trying to set its value, the value of the string object is incorrect. The
    difference between my
    previous use and what I am doing no is that before I was creating strings
    that might be only 8 - 10 characters long
    now they can be as long as 20 - 30 characters. Apparently my problem lies in
    the length of the string and resizing it.

    If in my code I use the constructor -

    string string2("This is a test");

    the value is what I would expect, however if I call the constructor with a
    longer string of say 20 or 30 characters, the string value is not the same
    as the value in the constructor.

    For example -

    string string2("This is a test of a much longer string that will fail");

    If I create the string and try to either call the resize function and then
    assign(or append, or copy) the value into the string, the result is still
    the same. I have read through the documentation for my compiler (MS Visual
    Studio .Net ... sorry) and reviewed several websites and books. I don't see
    what I am missing. Could somebody post some pseudo code that would
    demonstrate how to create a string, resize it to a set length and then set
    its value? My thinking is it should be something like this.... but it
    doesn't work.

    // Assuming testString is pointing to a string of char that is = to
    length.

    void SetStringValue(char* testString, int length) {
    string test;
    test.resize(length);
    test = testString;
    cout << string;
    }
    Doug Goulden, Feb 19, 2005
    #1
    1. Advertising

  2. Doug Goulden wrote:
    > void SetStringValue(char* testString, int length) {
    > string test;
    > test.resize(length);
    > test = testString;
    > cout << string;
    > }
    >
    >
    >
    >


    resize() only works on the string already inside the std::string object.
    Correct code would be to first copy the string and then resize it.

    test = testString;
    if(test.size() > length)
    test.resize(length);

    Note that the if statement is necessary because .resize() will, when
    strlen(testString) < length, copy n characters to fill the buffer to
    this size. The character defaults to '\0' (null).

    This is not the only way to do it either.
    Kurt Stutsman, Feb 19, 2005
    #2
    1. Advertising

  3. "Doug Goulden" <> wrote...
    >I have an application library that I have been using for some time
    > [...] Could somebody post some pseudo code that would
    > demonstrate how to create a string, resize it to a set length and then set
    > its value? [...]


    You have VC++, just look at their implementation of CString. Besides,
    many books have some kind of 'string' class implemented as an example
    of using C++ features. Another besides, the template 'basic_string'
    (part of the Standard library) is also usually available in source
    form, just look at <string> header.

    V
    Victor Bazarov, Feb 19, 2005
    #3
  4. Doug Goulden

    Doug Goulden Guest

    Ahh wonderful observation I made .... I went ahead and actually looked at
    the value that was printed out in a test application, and it was correct.
    Microsoft's wonderful IDE seems to have problems with displaying the value
    if there are more than 15 characters in the string.Thank you for the help...
    I guess it just goes to show you don't wanna believe everything MS tells you
    ;)

    "Kurt Stutsman" <> wrote in message
    news:qWMRd.1583$...
    > Doug Goulden wrote:
    > > void SetStringValue(char* testString, int length) {
    > > string test;
    > > test.resize(length);
    > > test = testString;
    > > cout << string;
    > > }
    > >
    > >
    > >
    > >

    >
    > resize() only works on the string already inside the std::string object.
    > Correct code would be to first copy the string and then resize it.
    >
    > test = testString;
    > if(test.size() > length)
    > test.resize(length);
    >
    > Note that the if statement is necessary because .resize() will, when
    > strlen(testString) < length, copy n characters to fill the buffer to
    > this size. The character defaults to '\0' (null).
    >
    > This is not the only way to do it either.
    Doug Goulden, Feb 19, 2005
    #4
  5. Doug Goulden

    Doug Goulden Guest

    Thank you for the reply, I finally figured out that the problem lies within
    the IDE display of the parameters in the function. In my class I was
    monitoring an intermediate output while I was debugging some XML input.
    Apparently if there are more than 15 characters the IDE chokes on the string
    object. When I output the value of the string using cout, it was actually
    correct. I couldn't understand why the basic_string class would be able to
    allocate memory on its own and yet not resize itself.....


    "Victor Bazarov" <> wrote in message
    news:...
    > "Doug Goulden" <> wrote...
    > >I have an application library that I have been using for some time
    > > [...] Could somebody post some pseudo code that would
    > > demonstrate how to create a string, resize it to a set length and then

    set
    > > its value? [...]

    >
    > You have VC++, just look at their implementation of CString. Besides,
    > many books have some kind of 'string' class implemented as an example
    > of using C++ features. Another besides, the template 'basic_string'
    > (part of the Standard library) is also usually available in source
    > form, just look at <string> header.
    >
    > V
    >
    >
    Doug Goulden, Feb 19, 2005
    #5
  6. Doug Goulden

    Alan Krueger Guest

    Doug Goulden wrote:
    > Ahh wonderful observation I made .... I went ahead and actually looked at
    > the value that was printed out in a test application, and it was correct.
    > Microsoft's wonderful IDE seems to have problems with displaying the value
    > if there are more than 15 characters in the string.Thank you for the help...
    > I guess it just goes to show you don't wanna believe everything MS tells you


    http://support.microsoft.com/default.aspx?scid=kb;en-us;326616
    Alan Krueger, Feb 21, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    725
    Mladen Adamovic
    Dec 4, 2003
  2. Purush
    Replies:
    4
    Views:
    1,610
    Purush Rudrakshala
    Apr 13, 2005
  3. E11
    Replies:
    1
    Views:
    4,717
    Thomas Weidenfeller
    Oct 12, 2005
  4. christopher diggins
    Replies:
    16
    Views:
    737
    Pete Becker
    May 4, 2005
  5. Joseph Turian
    Replies:
    5
    Views:
    578
Loading...

Share This Page