473,772 Members | 2,965 Online
Bytes | Software Development & Data Engineering Community
+ 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(len gth);
test = testString;
cout << string;
}


Jul 23 '05 #1
5 1775
"Doug Goulden" <jd*******@char termi.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(len gth);
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(len gth);

Note that the if statement is necessary because .resize() will, when
strlen(testStri ng) < 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*******@NOSP AM.sbcglobal.ne t> wrote in message
news:qW******** *********@newss vr14.news.prodi gy.com...
Doug Goulden wrote:
void SetStringValue( char* testString, int length) {
string test;
test.resize(len gth);
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(len gth);

Note that the if statement is necessary because .resize() will, when
strlen(testStri ng) < 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.********@com Acast.net> wrote in message
news:OM******** ************@co mcast.com...
"Doug Goulden" <jd*******@char termi.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
2392
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 me. This was inspired by Exercise 7 and Programming Problem 8 in Chapter 3 of our text. I have done Exercise 7 for you: Below you will find the ADT specification for a string of characters. It represents slightly more that a minimal string...
0
1491
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 out there can help, even come up with a better way of doing this.. i have 2 files.. here is my first file.. // a library assembly i.e. dll
6
4997
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 for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
16
2011
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 learning and really need some help bad. Public Shared Function GetAll() As ArrayList Dim dsCustomer As New DataSet() Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _ "FROM CustomerTable" Try
1
2183
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 <string>
2
2757
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 <string>
6
4031
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 types of objects (classes) can only be defined in Class modules.
8
29168
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 classes: Card.java, Deck.java and DeckTester.java. The specification for each class is given below. Card.Java This is a simple class that represents a playing card. Card has two attributes: • rank which is a String that represents the value...
2
2345
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 don't do that I am sure that I can not finish that before submited date. I write my assignment here also I write my codes please help me and guide me with writting the enough describe. thnaks for your help.
2
3819
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
9620
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
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 we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.