By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

Help with string class

P: n/a
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;
}


Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Doug Goulden" <jd*******@chartermi.net> 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
Jul 23 '05 #2

P: n/a
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.
Jul 23 '05 #3

P: n/a
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" <ks*******@NOSPAM.sbcglobal.net> wrote in message
news:qW*****************@newssvr14.news.prodigy.co m...
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.

Jul 23 '05 #4

P: n/a
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" <v.********@comAcast.net> wrote in message
news:OM********************@comcast.com...
"Doug Goulden" <jd*******@chartermi.net> 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

Jul 23 '05 #5

P: n/a
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...b;en-us;326616
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.