473,408 Members | 2,734 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,408 software developers and data experts.

std::string, Append a number

The following isn't doing what I thought it would do:

#include <iostream>

int main()
{
int k = 56;

std::string blah(" ");

blah += k;

std::cout << "\n\n" << blah << "\n\n";
}

How do I simply append a number to an std::string?
As in:

"Amount: "

becoming

Amount: 56"

?

Thanks,

-JKop
Jul 22 '05 #1
4 7457

"JKop" <NU**@NULL.NULL> wrote in message
news:%J*******************@news.indigo.ie...
The following isn't doing what I thought it would do:

#include <iostream>

int main()
{
int k = 56;

std::string blah(" ");

blah += k;

std::cout << "\n\n" << blah << "\n\n";
}

How do I simply append a number to an std::string?
As in:

"Amount: "

becoming

Amount: 56"

You could try something like this:

#include <string>
#include <iostream>
#include <sstream>

int main()
{
std::string s("Amount:");
int tot = 42;
std::ostringstream oss;
oss << s << ' ' << tot << '\n';
std::cout << oss.str();
}

Regards,
Sumit.
--
Sumit Rajan <sumitrajan AT alexandria DOT cc>
Jul 22 '05 #2
"Sumit Rajan" <su********@yahoo.com> wrote in news:2r8b97F176sjuU1@uni-
berlin.de:

"JKop" <NU**@NULL.NULL> wrote in message
news:%J*******************@news.indigo.ie...
The following isn't doing what I thought it would do:

#include <iostream>

int main()
{
int k = 56;

std::string blah(" ");

blah += k;

std::cout << "\n\n" << blah << "\n\n";
}

How do I simply append a number to an std::string?
As in:

"Amount: "

becoming

Amount: 56"

You could try something like this:

#include <string>
#include <iostream>
#include <sstream>

int main()
{
std::string s("Amount:");
int tot = 42;
std::ostringstream oss;
oss << s << ' ' << tot << '\n';
std::cout << oss.str();
}


... or look up the lexical_cast<> item in Boost (http://www.boost.org)...
(which I think does the same thing :) )
Jul 22 '05 #3
JKop <NU**@NULL.NULL> wrote in message news:<%J*******************@news.indigo.ie>...
The following isn't doing what I thought it would do:

#include <iostream>

int main()
{
int k = 56;

std::string blah(" ");

blah += k;

std::cout << "\n\n" << blah << "\n\n";
}

How do I simply append a number to an std::string?
As in:

"Amount: "

becoming

Amount: 56"

?

Thanks,

-JKop


Use ostringstream to convert the int to a string type:
main()
{
int k=56;
std::string blah("woot ");

std::ostringstream os;
os << k;
std::string k_str = os.str(); //retrieve as a string

blah += k_str;
std::cout << blah << "\n\n";
}
Jul 22 '05 #4
Andre Kostur <nn******@kostur.net> wrote in message news:<Xn*******************************@207.35.177 .135>...
.. or look up the lexical_cast<> item in Boost (http://www.boost.org)...
(which I think does the same thing :) )


Yes, boost::lexical_cast is nicer, it hides the stream usage.

Specific reference here: http://www.boost.org/libs/conversion/lexical_cast.htm

This is the code, FYI, using your example.

#include <iostream>
#include <boost/lexical_cast.hpp>

int main()
{
int k = 56;

std::string blah(" ");

blah += boost::lexical_cast<std::string>(k);

std::cout << "\n\n" << blah << "\n\n";
}

Regards,

Matt Hurd
www.hurd.com.au
Jul 22 '05 #5

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

Similar topics

3
by: Alexandros Frantzis | last post by:
Hello, I am trying to read a big file into a string. AFAIK std::string can read in words or "lines" from a stream. So one option is to continuously append all the "lines" from the file to the...
2
by: Matthias Käppler | last post by:
Hi, I just browsed libstdc++6-doc and stumbled over a string operation I haven't noticed before: string::push_back. If for example I want to append single characters to an std::string, which...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
13
by: Pep | last post by:
I have to interface to an older library that uses strings and there is no alternative. I need to pass a string that is padded with null bytes. So how can I append these null bytes to the...
8
by: vidya.bhagwath | last post by:
Hello Experts, I am using std::string object as a member variable in one of the my class. The same class member function operates on the std::string object and it appends some string to that...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
13
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
25
by: Bala2508 | last post by:
Hi, I have a C++ application that extensively uses std::string and std::ostringstream in somewhat similar manner as below std::string msgHeader; msgHeader = "<"; msgHeader += a; msgHeader...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.