Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help with string class

Reply
Thread Tools

Help with string class

 
 
Doug Goulden
Guest
Posts: n/a
 
      02-19-2005
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;
}




 
Reply With Quote
 
 
 
 
Kurt Stutsman
Guest
Posts: n/a
 
      02-19-2005
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-19-2005
"Doug Goulden" <(E-Mail Removed)> 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


 
Reply With Quote
 
Doug Goulden
Guest
Posts: n/a
 
      02-19-2005
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" <(E-Mail Removed)> wrote in message
news:qWMRd.1583$(E-Mail Removed) 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.



 
Reply With Quote
 
Doug Goulden
Guest
Posts: n/a
 
      02-19-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Doug Goulden" <(E-Mail Removed)> 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
>
>



 
Reply With Quote
 
Alan Krueger
Guest
Posts: n/a
 
      02-21-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Generic class literals - e.g,, Class<Map<String, Integer>>.class Purush Java 4 04-13-2005 08:40 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments