Help with string class

D

Doug Goulden

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;
}
 
K

Kurt Stutsman

Doug said:
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.
 
V

Victor Bazarov

Doug Goulden said:
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
 
D

Doug Goulden

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
;)
 
D

Doug Goulden

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 said:
Doug Goulden said:
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
 
A

Alan Krueger

Doug said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top