473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Add an integer to a string without using std::stringstream

If std::stringstreams are unavailable, how would you go about adding
an integer (or unsigned integer) to a string?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
5 2370

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c7**********@chessie.cirr.com...
If std::stringstreams are unavailable, how would you go about adding
an integer (or unsigned integer) to a string?


sprintf the integer to a C string and then append that C string to the C++
string. That would probably be the way I would do it in any case.

John
Jul 22 '05 #2

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c7**********@chessie.cirr.com...
If std::stringstreams are unavailable, how would you go about adding
an integer (or unsigned integer) to a string?


#include <cstdio>
#include <iostream>
#include <limits>
#include <string>

int main()
{
std::string s("Hello");
int i(42);
char *tmp = new char[std::numeric_limits<int>::digits10 + 2];
std::sprintf(tmp, "%d", i);
s += tmp;
std::cout << s << '\n'; /* prints "Hello42"
delete[] tmp;
return 0;
}

-Mike
Jul 22 '05 #3
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
If std::stringstreams are unavailable, how would you go about adding
an integer (or unsigned integer) to a string?


In addition to sprintf as others point out, there is also istrstream -- need
to #include <strstream.h>. Not sure if it is deprecated or even still part
of the standard, but imagine it is fine.
Jul 22 '05 #4
Christopher Benson-Manica wrote:
If std::stringstreams are unavailable, how would you go about adding
an integer (or unsigned integer) to a string?


#include <limits>
#include <string>
#define X *--o='0'+(x%10);x/=10;
#define O while(x){*--o=',';X if(x)X if(x)X}

template <typename Integer>
void append_int (Integer x, std::string & s)
{
char buffer [std::numeric_limits <Integer>::digits10 * 4 / 3 + 2];
char * o = buffer + sizeof buffer;

if(x<0){if((x=-(x+1))%10-9){*--o='1'+(x%10);x/=10;}
else{*--o='0';x/=10;++x;}X if(x)X O*--o='-';}
else if(x){X if(x)X if(x)X O}else{*--o='0';}

s.append (o, buffer + sizeof buffer);
}

--
Regards,
Buster.
Jul 22 '05 #5
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message news:<jC********************@bgtnsc04-news.ops.worldnet.att.net>...
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
If std::stringstreams are unavailable, how would you go about adding
an integer (or unsigned integer) to a string?


In addition to sprintf as others point out, there is also istrstream -- need
to #include <strstream.h>. Not sure if it is deprecated or even still part
of the standard, but imagine it is fine.


std::[io]strstream, in <strstream>, is standard but deprecated.
Jul 22 '05 #6

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

Similar topics

5
by: Ellarco | last post by:
Im sorry for asking a question that is surely in the archives somewhere, but I have been unable to locate it. Its about string memory management. I need to dynamically construct a C-style string...
4
by: Dylan | last post by:
Hi again, In the following program I expected step 3 to assign the values 1 and 2 to iVal1 and iVal2 yet it appears ss.seekg(0, std::ios::beg) does not move the read position back to the...
5
by: Mr Fish | last post by:
Is it possible for me to record floats in a std::stringstream with 100% accuracy? If so how? Here's a little prog that demonstrates how the default ss loses accuracy ...
5
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
1
by: magix | last post by:
I got this reply in my previous post a month ago: May I know, how can I automatically create the folder if it doesn't exist ? In previous reply, it said: -------------------------...
0
by: ziyanjoe | last post by:
Hi! I am writing a program for the robot that runs as a daemon on a linux machine. Since debug output cannot be seen on stdout, I want to create an iostream to handle all the output messages....
7
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
7
by: Grey Alien | last post by:
Does *ANYONE* in here know how I may parse the various date/time 'elements' from a string?. The input string has the ff format: 'YYYY-MM-DD HH:MM:SS AM'
3
by: PengYu.UT | last post by:
Hi, The following code would output "error". I don't understand why there is such error. Could you please help me? Thanks, Peng #include <string> #include <sstream>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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...
0
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
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...

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.