473,466 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with string class

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
5 1758
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
0
by: Shaggyh | last post by:
hi im needing help with a program im writing to do subnetting i was on before about it and i got some help. the code below wont work for me and i cant think of why not. i was wondering if anyone...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
16
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just...
1
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
2
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
8
by: garyrowell | last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received. The program This week you are going to write three...
2
by: bmbvm5 | last post by:
Hello; I am beginner in java and I should submit one java program in early day. I try to write this program more then ten time but every time I find one broblem. Please help me because if you...
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null c,null,D
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.