473,790 Members | 3,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting integer to a string(without the help of stringstreamvar iable)

Hi,
I am using a string variable in which I do lot of appending. The
only difficulty I am facing as of now is appending a integer/float
value to this variable. Although I can accomplish this task using a
stringstream variable, it makes my life difficult as it involves 2
function call(one for adding the information to stringstream variable
and the other for copying that value in to string variable) with the
additional burden of resetting this stringstream variable.

Is there anyway I can accomplish this task in a much simpler
way. Thanx in advance.

Feb 28 '08 #1
4 1872
On Feb 27, 10:22 pm, mthread <rjk...@gmail.c omwrote:
Hi,
I am using a string variable in which I do lot of appending. The
only difficulty I am facing as of now is appending a integer/float
value to this variable. Although I can accomplish this task using a
stringstream variable, it makes my life difficult as it involves 2
function call(one for adding the information to stringstream variable
and the other for copying that value in to string variable) with the
additional burden of resetting this stringstream variable.

Is there anyway I can accomplish this task in a much simpler
way. Thanx in advance.
Check out the library provided by boost.org

There is a lexical_cast<>( ) function.

It uses stringstream underneath but provides a nice interface to it.
Feb 28 '08 #2
mthread wrote:
Hi,
I am using a string variable in which I do lot of appending. The
only difficulty I am facing as of now is appending a integer/float
value to this variable. Although I can accomplish this task using a
stringstream variable, it makes my life difficult as it involves 2
function call(one for adding the information to stringstream variable
and the other for copying that value in to string variable) with the
additional burden of resetting this stringstream variable.

Is there anyway I can accomplish this task in a much simpler
way. Thanx in advance.
AFAIK the only alternative *standard* way to do that would be to use
std::sprintf(), but that would probably not be any simpler than using
stringstreams. On the contrary, it would probably be more complicated
because you would have to guess how many characters std::sprintf() will
write, and make sure you allocate enough space for it. Then you have to
resize the string to fit the actual amount of characters written.
Feb 28 '08 #3
mthread a écrit :
Hi,
I am using a string variable in which I do lot of appending. The
only difficulty I am facing as of now is appending a integer/float
value to this variable. Although I can accomplish this task using a
stringstream variable, it makes my life difficult as it involves 2
function call(one for adding the information to stringstream variable
and the other for copying that value in to string variable) with the
additional burden of resetting this stringstream variable.

Is there anyway I can accomplish this task in a much simpler
way. Thanx in advance.
If you do a lot of appending, using a stringstream from the very
beginning seems logical. And with it, you could control the formating of
your numerical values (which is not the case with boost::lexical_ cast<>).
Michael
Feb 28 '08 #4
mthread wrote:
Hi,
I am using a string variable in which I do lot of appending. The
only difficulty I am facing as of now is appending a integer/float
value to this variable. Although I can accomplish this task using a
stringstream variable, it makes my life difficult as it involves 2
function call(one for adding the information to stringstream variable
and the other for copying that value in to string variable) with the
additional burden of resetting this stringstream variable.

Is there anyway I can accomplish this task in a much simpler
way. Thanx in advance.
I also ran into this problem a bit and I finally made a template.

template<typena me T, typename F T StrmConvert( const F from )
{
std::stringstre am temp;
temp << from;
T to = T();
temp >to;
return to;
}

template<typena me Fstd::string StrmConvert( const F from )
{
return StrmConvert<std ::string>( from );
}

Basically this allows me to do things like:

std::string MyString;
MyString += "Some Value: " + StrmConvert( someint ) + " some float:" +
StrmConvert( somefloat );

etc.. All in one line.

Note there is no error checking, so say, for example, you wanted to pull a
number from a string:

std::string Data = "abc";
int MyInt = StrmConvert<int >( Data );

Data would contain 0 since the variable in StrmConert is default
constructed. It will not throw or even indicate there was an error other
than the default constructed value. This works for me. If it doesn't work
for you, check the status of the stringstream temp in StrmConvert and if it
is not okay throw something.
--
Jim Langston
ta*******@rocke tmail.com
Feb 28 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
4076
by: Markus Dehmann | last post by:
The following if condition if((stream = fmemopen((void*)str, strlen(str), "r")) == NULL){ // ... } gives me a warning: assignment makes pointer from integer without a cast But only when I compile it with gcc. With g++ it's fine. Why does the warning show up and how can I get rid of it (using gcc)?
2
14358
by: francescomoi | last post by:
Hi. I'm trying to compile this piece of source: ------------------------------------------- int id; while(row1 = mysql_fetch_row(rs1)) { id = atoi((int)row1); -----------------------------------
20
17243
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp: 0.0.0.0--->I want to tokenize the string using delimiter as as dot. Regards
6
7475
by: DaTurk | last post by:
Hi, I have several interfaces in CLI that I access via c#. My problem is, is that down in the unmanaged c++, which the CLI lies on top of, I have a lot of c_str() happening. But all of my methods in CLI return System::String^. I originally just gcnew'd System::String^ passing in the c_str(). But I can't really have as many gcnew's as I'm using for overhead and for fear of leaks. So my question is this, how can I get the char*...
3
17220
by: mrmattborja | last post by:
Hello, Here is a program I'm playing around with for fun in the process of learning C. The objective is to create a function filesize() and call it from within the main() section to retrieve the size in bytes of a file that I give it. #include <stdio.h> long filesize(f) { FILE *fp;
6
3554
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as I'm avoiding the use of fscanf and fgets to read in lines. I don't want to have to specify a temporary char* buffer to read in each line, and then have to concern myself with the (remote) possibility of overflows or with increasing the buffer...
1
2811
by: abcd | last post by:
Has anyone seen this error before: wrap_EncryptByteArray() argument 1 must be string without null bytes, not str I am having a hard time finding anything about it.
10
9506
drhowarddrfine
by: drhowarddrfine | last post by:
warning: assignment makes pointer from integer without a cast I get that when I compile a C file where the function is defined as this: char **getvars() and the calling function has this declared: char **s; and I write: s=getvars();
1
11745
by: dn6326 | last post by:
Hi there, im pretty new to this forum and would be grateful of some help. I've only started programming this year and am pretty lazy with it so please bare with me if im acting like a bit of a noob... right basically im trying to define a struct that will hold names for a phonebook i.e. with a string and a number i have #included<stdlib.h> and everything else I need, and the 2 parts im struggling with are as follows typedef struct {char...
0
9512
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
10419
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10201
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
9987
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
9023
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
6770
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
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
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
2910
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.